[rspec-users] Surprising mock behavior
mark.thomson at ieee.org
Fri Oct 17 15:10:07 EDT 2008
Actually, this is evidently not the whole story. I actually have two
examples in the same spec, and I just realized that the other one has
instances of file.should_receive(:puts) that don't seem to suffer the
same limitation I described. So it looks like there is something about
the example I described that is causing it to behave strangely. Will
Mark Thomson wrote:
> I'm writing my first controller spec - for a controller that already
> exists, and in the process have observed some behavior I find a little
> surprising. I'd like to know whether I'm interpreting the situation
> In my controller I have some stuff written to a file, i.e.
> file = File.new(...)
> file.puts "a string"
> file.puts "another string"
> In my spec I mock the file object -
> file = mock('file')
> and then check that the expected messages are being received -
> file.should_receive(:puts).with("a string").once
> file.should_receive(:puts).with("another string").once
> Here's what I'm puzzled about. If I don't include the expectation for
> the first string in the spec, the spec will fail the expectation for
> the second string. It seems as if "should_receive" is queuing up the
> messages that come into the file object and when it tests an
> expectation it just looks at the next one in line. If it doesn't match
> then the expectation will fail.
> Is that really what happens? Or have I missed some important insight?
> Seems counterintuitive to me. What is strange is that I was working
> away on this yesterday and just picking out a number of particular
> puts to test that I was concerned about. It seemed to be working the
> way I expected it to. Then late yesterday I started seeing some spec
> failures I didn't understand and my investigation led me to the
> conclusion above.
> Interested to know if what I think I'm seeing is consistent with other
> people's experience and with the intended behavior of mocks.
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users