[rspec-devel] [ rspec-Bugs-13370 ] Weird mock expectation error

noreply at rubyforge.org noreply at rubyforge.org
Sat Sep 15 17:41:19 EDT 2007


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

Category: mock module
Group: None
>Status: Closed
>Resolution: Accepted
Priority: 3
Submitted By: Yurii Rashkovskii (yrashk)
Assigned to: Nobody (None)
Summary: Weird mock expectation error

Initial Comment:
While running code that contains

@user.should_receive(:assigned_to?).with(@issue).any_number_of_times.and_return true

# @issue is a mock as well

I'm getting weird failure:

Mock 'User_1263' expected :assigned_to? with (#<Issue:0x18b79b8 @name="Issue_1261">) but received it with (#<Issue:0x18b79b8 @name="Issue_1261">)

Any ideas?

P.S. trunk rspec

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

>Comment By: David Chelimsky (dchelimsky)
Date: 2007-09-15 21:41

Message:
Fixed in r2565 - thanks to Mike Mangino for the patch.

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

Comment By: Yurii Rashkovskii (yrashk)
Date: 2007-08-26 12:07

Message:



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

Comment By: David Chelimsky (dchelimsky)
Date: 2007-08-25 19:31

Message:
Hey Yurii: there is no plan, so feel free to submit a patch if you can figure something out that makes sense.

The problem is that Rails assoc proxy objects lie about who they are and ActiveRecord does type checking on associated collections. RSpec's mock_model creates an object that, similar to Rails, lies about its identity in order to pass Rails' type checking.

So you're getting two DIFFERENT OBJECTS that are both lying about their identity. RSpec is doing its job correctly (they really are two different objects and your spec *should* fail), but because both objects are impersonating something else, you get confusing/misleading feedback.

FYI - you'd get the same feedback from any mock framework (try it w/ mocha and/or flexmock).

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

Comment By: Yurii Rashkovskii (yrashk)
Date: 2007-08-25 19:12

Message:
Any chance this will be fixed? Could I help? I urgently need to get this fixed or 
workarounded :)

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

Comment By: David Chelimsky (dchelimsky)
Date: 2007-08-25 19:05

Message:
This has come up recently. If @issue is a mock, then make sure that the object it is being compared to is not a rails AssociationProxy, which will tell you that it is the model object even though it is not.

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

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


More information about the rspec-devel mailing list