我有一个 Rails 控制器动作要测试。在那个动作中,一个方法 User.can? 使用不同的参数多次调用。在其中一个测试用例中,我想确保调用 User.can?('withdraw') 。但我不关心 User.can 的调用?与其他参数。
def action_to_be_tested
...
@user.can?('withdraw')
...
@user.can?('deposit')
...
end
我在测试中尝试了以下内容:
User.any_instance.expects(:can?).with('withdraw').at_least_once.returns(true)
但测试失败,消息表明意外调用 User.can?('deposit')。如果我使用参数“存款”添加另一个期望,则测试通过。但是我想知道是否有任何方法可以让我只关注带有“withdraw”参数的调用(因为其他调用与这个测试用例无关)。