any_instance is not instance-agnostic when using receiver count expectation
Created by: wolframarnold
I'm trying to set up a spec like this:
class Calc
end
context "any_instance with receiver count expectation" do
before do
Calc.any_instance.should_receive(:add).twice
end
it 'should work with multiple instances' do
Calc.new.add(1,2)
Calc.new.add(2,3)
end
This doesn't work, it generates an error:
RSpec::Mocks::MockExpectationError: The message 'add' was received by #<Calc:0x884cdc8> but has already been received by #<Calc:0x884f9d8>
I'm concluding that when using a receiver count expectation any_instance
is specific to a given instance, counter to the assumption that any_instance
really should work with any instance.
What I actually want to do is to be able to define multiple argument expectations with this, along the lines of:
Calc.any_instance.should_receive(:add).with(1,2).and_return(3)
Calc.any_instance.should_receive(:add).with(2,3).and_return(5)
That totally breaks down. The simple calculator example here is for illustration only, my actual issue is more complex and I can't easily compute the return value, but have to rely on table look-ups. In other words, I've not found a suitable work-around and I really need this behavior as described above, if it's supposed to be supported (which I'm not sure of).
I don't know if it matters: I'm doing this with Rails 3, RSpec 2.6, and Ree Ruby 1.8.7