0
How to do complex expectations with RSpec
Creat
29/11/2011 11:30:03
per
Roger Campos
The thing what to do if we can set an expectation for a certain object and get control over what it return on two consecutive calls?
Well, we can easily do something like:
@object.should_receive(:method_name).twice.and_return([1, 2])
Then the first call to @object.method_name will return 1 and the second one 2.
But things can get more complicated if we want, for instance, raise and error on the first call and return '43' on the second one. This is a little more tricky to do, but we can acomplish it with:
@object.should_receive(:method_name).at_most(1).times.and_raise(ArgumentError)
@object.should_receive(:method_name).once.and_return(43)
The first expectation will last only for 1 call, and then the second expectation we make take place again only for 1 run.
There is an example in this gist: https://gist.github.com/1389066