[rspec-users] a MockExpectationError problem

Phillip Koebbe phillipkoebbe at gmail.com
Sun Apr 25 19:22:31 EDT 2010


  On 2010-04-25 5:25 PM, Patrick J. Collins wrote:
> Here is a gist of my code:
> http://gist.github.com/378778
>
>> Use stub when you just want to provide the plumbing. Use should_receive when
>> you are setting an expectation. As an example, you could do
>> Photo.stub(:create) just to provide the method to avoid errors when your code
>> calls it. But if you actually want to verify that your code calls it, then do
> Well, as you can see by my code, I am actually doing a loop
>
> @card.addresses.each ....  This is calling the @country = get_country ||
> Country.find_by_name("United States")
>
> So-- the number of times .find_by_name needs to be intercepted by
> .should_receive is dependent on the number of addresses in a vcard.

And this would be the difference between stubbing and expecting. Do you 
need to set an expectation that it gets called a certain number of 
times? If not, just stub it (which is what you did and the error went 
away). Only use should_receive when you want to verify that a method is 
actually being called in your code. I generally do that when I want to 
make sure different branches of execution are followed correctly.
> This was the problem I didn't know how to address, and using stub! resolved it,
> but like I said--  I didn't know if there's a more appropriate way to do it.
>
> Patrick J. Collins
> http://collinatorstudios.com

Regarding your original problem of the mocking error, have you debugged 
into the FlexImage code? In looking at your gist, you have this:

         # @photo = mock_model(Photo, {:image_file_string => photo_content})

which says "create a mock object based on the Photo model, and if 
#image_file_string gets called on that object, return photo_content". 
Then you have

         # Photo.should_receive(:create).and_return(@photo)

which obviously says "Photo should receive a call to the .create method, 
and when it does, return @photo". The potential problem that I see 
(though I may not be understanding something correctly), is when you 
call Photo.create, .image_file_string= is being called in the FlexImage 
codebase. I'm wondering if your issue might be somewhere down in there. 
I'd do some more digging, but I have to run right now.

Good luck!

Peace,
Phillip


> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list