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

Bas Vodde basv at odd-e.com
Sun Apr 15 06:32:49 UTC 2012


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



More information about the rspec-users mailing list