[rspec-users] a MockExpectationError problem

Patrick J. Collins patrick at collinatorstudios.com
Sun Apr 25 17:25:40 EDT 2010

On Sun, 25 Apr 2010, Phillip Koebbe wrote:
> See http://gist.github.com/378489 for how I ended up specing a file upload.
> This isn't the exact same situation, but maybe you will get some ideas that
> will help. And there may be a better way, but this seems to work and is fairly
> easy for me understand.

Yeah, I see what you're doing, but I don't quite get how I could apply this to
what I am doing.

I have a method that creates a contact and then creates a photo and assigns it
to that contact.

So my problem comes from doing (in my model method):

@contact.photo = Photo.create(...)

...  Although the code works fine in the real world app use, the spec fails
because it's trying to do something with "[]=" .. which I don't get.


Also, I have a couple side questions-- 

1) is there some sort of caching done to before(:all/:each) blocks?

I had a before block that set an instance variable to something, and then I
removed it-- and was getting weird behavior, and after doing some inspecting I
found that the instance variable was still set-- even though it was not set
anywhere in the code...  I had to put a blank before block in there to get that
behavior to stop...  Just wondered if this is normal?


2)  I still am struggling with mock vs stub-- or I should say stub! vs.
should_receive.  Initially I had something like this:

before(:all) do
  foo = mock_model)

it "blah" do
  3.times {  @blah.do_something }

it "blah2" do
  3.times { @blah.do_something }

(blah model)
def do_something
  @foo = Foo.find_by_name("bar")

This wasn't working, because apparently it's only good for one usage.  I was
thinking that declating "should_receive" would mean that anytime Foo gets
.find_by_name called on it, it will have that behavior.  I ended up changing
that to a before(:each), and then I got errors like expected it 1 time but got
it 3 times....

So not knowing how to deal with that, I changed .should_receive to .stub! and
then the errors went away.

Not sure if that was the right thing to do in that case.

Patrick J. Collins

More information about the rspec-users mailing list