本文介绍了仅用于特定参数的 Rspec 存根方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法只为特定参数存根方法.像这样
Is there a way to stub method for only specific arguments. Something like this
boss.stub(:fire!).with(employee1).and_return(true)
如果任何其他员工被传递给 boss.fire!
方法,我会得到 boss received unexpected message
错误,但我真正想要的只是覆盖具体论证的方法,留给所有其他人.
If any other employee is passed to boss.fire!
method, I'll get boss received unexpected message
error, but what I would really like is just to override the method for specific argument, and leave it be for all others.
知道如何做到这一点吗?
Any ideas how this can be done?
推荐答案
您可以为将调用原始实现的 fire!
方法添加默认存根:
You can add a default stub for the fire!
method which will call original implementation:
boss.stub(:fire!).and_call_original
boss.stub(:fire!).with(employee1).and_return(true)
Rspec 3 语法 (@pk-nb)
Rspec 3 Syntax (@pk-nb)
allow(boss).to receive(:fire!).and_call_original
allow(boss).to receive(:fire!).with(employee1).and_return(true)
这篇关于仅用于特定参数的 Rspec 存根方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!