[rspec-users] RSpec exactly behavior (it doesn't exactly fail)

David Chelimsky dchelimsky at gmail.com
Sun Apr 15 21:48:06 UTC 2012


On Sunday, April 15, 2012 at 1:32 AM, Bas Vodde wrote:
> 
> Hiya all,
> 
> I've got a quick question related to RSpec. I was test-driving some code and ended up in an endless loop. I was surprised by this, but traced it down to the mock not failing on additional calls but only in the end. Let me explain.
> 
> I was writing code like this:
> 
> subject.wrapper.should_receive(:window_list).exactly(4).times.and_return {
> counter = counter + 1
> counter >= 4 ? [ "new window" ] : []
> }
> 
> The idea was that it would call the code-block 4 times exactly and then return a new value (and thus stop calling it). As the code to implement wasn't there yet, it led to a recursive call. I had expected RSpec to stop after 4 calls though, as I had instructed the mock that I expected exactly 4 calls.
> 
> I added a new test in RSpec itself in precision_counts_spec.rb:
> 
> it "fails when a method is called more than n times, but fails within the method call" do
> @mock.should_receive(:random_call).exactly(1).times
> lambda do
> @mock.random_call
> @mock.random_call
> end.should raise_error(RSpec::Mocks::MockExpectationError) 
> end
> 
> which failed :( (or it failed to fail and therefore failed!)
> 
> It would be nice if it would fail. Is there any reason for not failing already at this point in time?
> 
> (I'm using RSpec 2.6-0. I quickly browsed the latest and didn't see this changed)
> 
> Thanks,
> 
> Bas
There is no philosophical reason for this to happen, and there are other types of failures that do fail-fast (e.g. obj.should_receive(:bar).with(1,2) fails immediately if it receives :bar with any other args).

Please submit this to https://github.com/rspec/rspec-mocks/issues and I'll start looking into a fix.

Cheers,
David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120415/936ae615/attachment.html>


More information about the rspec-users mailing list