[rspec-devel] [ rspec-Bugs-7565 ] Subsequent stub! calls for method fail to override the first call to method

noreply at rubyforge.org noreply at rubyforge.org
Sat Jan 6 09:03:58 EST 2007


Bugs item #7565, was opened at 2007-01-04 18:14
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=7565&group_id=797

Category: mock module
Group: None
>Status: Closed
>Resolution: Accepted
Priority: 3
Submitted By: Chris Hoffman (hoffman_c)
>Assigned to: David Chelimsky (dchelimsky)
Summary: Subsequent stub! calls for method fail to override the first call to method

Initial Comment:
  My collegue and I found a condition where subsequent calls to foo.stub!(:bar) did not override the original stubbed method, and did so silently (i.e., without informing the programmer by raising an exception).  Upon further inspection, we found that the @stubs instance variable (mock_handler.rb) was being appended to in the add_stub method call.  Then, when it comes time to execute this stubbed method, an array find is used (I forget where in the source) to find the appropriate return value.
  So, basically, the add_stub method call is dutifully adding a duplicate method to the array, and ignoring it by always retrieving the first stub matching the conditions.  This hardly seems to be desired behavior.  Would changing the @stubs variable to a hash be possible?  I would think such an approach better than executing an array find.
  What follows is a contrived spec to further elucidate our findings.  Obviously this example doesn't make much sense in practice, but we do need this functionality when setting up specs becomes more involved.  Thanks.

context 'A stub' do
  setup do
    @stub = mock("stub")
    @stub.stub!(:foo).and_return(1)
    @stub.stub!(:foo).and_return(2)
  end

  specify "should equal 2 when calling foo" do
    @stub.foo.should_eql 2
  end
end


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

>Comment By: David Chelimsky (dchelimsky)
Date: 2007-01-06 14:03

Message:
Fixed in trunk (rev 1361)

Stubs are now added to the front of the Array, so the one it finds will be the last one that was set. Perhaps not as elegant as using a Hash, but it keeps the code simpler and works just fine.

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

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


More information about the rspec-devel mailing list