[rspec-devel] [ rspec-Patches-14152 ] Output warning when method is defined on a nil object.

noreply at rubyforge.org noreply at rubyforge.org
Wed Oct 3 12:42:51 EDT 2007


Patches item #14152, was opened at 2007-09-22 18:35
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3151&aid=14152&group_id=797

Category: None
Group: None
Status: Open
Resolution: None
Priority: 3
Submitted By: Ian Leitch (idl)
Assigned to: David Chelimsky (dchelimsky)
Summary: Output warning when method is defined on a nil object.

Initial Comment:
This patch causes define_expected_method to output a warning when a method is defined on a nil object.

This patch allows me to quickly resolve failing specs caused by typos.

e.g.:

before do
  @obj = mock('some object')
  @ob.stub!(:do_stuff)
end

it "should do something" do
    some_trigger
end

This will result in:

Warning: 'do_stuff' defined on a nil object at foo.rb:123.

This patch highlights some nil object stubs in the rspec's own specs. Most appear to be where the author hasn't bothered to use the method created in the before block (or not realized they were referencing a nil object). Although, one case identified in passing_mock_argument_constraints_spec.rb is where the "handling non-constraint arguments" behavior does not define 'it_should_behave_like "mock argument constraints"'.

Cheers
Ian

----------------------------------------------------------------------

>Comment By: David Chelimsky (dchelimsky)
Date: 2007-10-03 16:42

Message:
Sure it's a valid use case! Any time you have code that has to interact with nil in some way, you might want to say nil.stub!(:some_message) or nil.should_receive(:some_message).

We mock/stub globals all the time. Think Rails finder methods. They get cleared out after each example, so while there is risk of odd behaviour within a given example (if you don't understand what you're doing), it doesn't bleed out to the rest of the system.

As for rejecting it - it hasn't been rejected yet partially or completely. I simply asked a question, which I now reiterate:

How would I specify nil.should_receive(:some_message).

----------------------------------------------------------------------

Comment By: Ian Leitch (idl)
Date: 2007-10-03 16:35

Message:
You wouldn't, is that even a valid use case?

Please correct me, but from what I understand, someone
stubbing on nil directly is being lazy and not bothering to
create mock objects. Also, stubbing on a global object like
that could lead to expected (and very hard to debug) behavior.

This patch has been very handy for myself and my fellow
employees, it'd be a shame if it were completely rejected.

----------------------------------------------------------------------

Comment By: David Chelimsky (dchelimsky)
Date: 2007-10-02 22:30

Message:
How would I specify this:

nil.should_receive(:some_message)

?????

----------------------------------------------------------------------

Comment By: Ian Leitch (idl)
Date: 2007-09-22 19:18

Message:
By the way, if you accept this patch I'll go ahead and fix
all the warnings in rspec's specs.

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3151&aid=14152&group_id=797


More information about the rspec-devel mailing list