[rspec-users] Surprising mock behavior
mark.thomson at ieee.org
Fri Oct 17 14:55:22 EDT 2008
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 -
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.
More information about the rspec-users