[rspec-devel] Fix for should == mock_model with ActiveRecord

Mike Mangino mmangino at elevatedrails.com
Sat Sep 15 10:57:49 EDT 2007

I've attached a fix for the problems using specs like

it "should belong to a user" do
   @comment.user.should == @user

where @user is a mock_model.

To summarize the issue,

rspec mocks use a proxy (__mock_proxy) to delegate methods to. When  
on the LHS, == is delegated to the proxy. On the RHS, however, we are  
comparing the mock itself to the proxy. There are a couple of  
potential fixes.

We could actually check to see if the RHS is a mock, and if so,  
compare our proxy to its proxy. Alternatively, we could take the  
naive solution and just reverse the comparison.

I've attached the patch. I'm open to other suggestions, but I would  
really like to see this fixed in some form.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: mock_model_fix.patch
Type: application/octet-stream
Size: 2483 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/rspec-devel/attachments/20070915/e43b611e/attachment.obj 
-------------- next part --------------

Mike Mangino

More information about the rspec-devel mailing list