Search code examples
rspec-mocks

Is it possible to expect an RSpec double be called twice with varying values?


On RSpec's mock docs I found
expect(double).to receive(:msg).exactly(3).times.and_return(value1, value2, value3)
# returns value1 the first time, value2 the second, etc`

If I do the same with parameters, for ex.
expect(double).to receive(:msg).exactly(3).times.with(value1, value2, value3)
RSpec naturally expects msg to be called with value1, value2, value3 three times.
Is there a way to say called the first time with value1, the second time with value 2, etc?


Solution

  • Try using .ordered, like this:

    expect(double).to receive(:msg).with(value1).ordered
    expect(double).to receive(:msg).with(value2).ordered
    expect(double).to receive(:msg).with(value3).ordered