Mocha: How to add expectation of a method when there are multiple invocations with different parameters
You can pass a block to with
and have that block inspect the arguments. Using that, you can construct a list of expected invocations:
invocations = ['withdraw', 'deposit']
User.any_instance.expects(:can?).at_most(2).with do |permission|
permission == invocations.shift
end
Each time can?
is called, Mocha will yield to the block. The block will pull the next value off the list of expected invocations and check it against the actual invocation.
I just found a workaround, by stubbing out invocations with irrelevant parameters:
User.any_instance.expects(:can?).with('withdraw').at_least_once.returns(true)
User.any_instance.stubs(:can?).with(Not(equals('withdraw')))
http://mocha.rubyforge.org/classes/Mocha/ParameterMatchers.html#M000023