node.js 使用Sinon时,如何替换stub实例中的stub函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33099863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When using Sinon, how to replace stub function in a stub instance?
提问by StevenR
If I have create a instance by var a = sinon.createStubInstance(MyContructor).
如果我创建了一个实例 var a = sinon.createStubInstance(MyContructor)。
How can I replace one of the stubbed function like var stub = sinon.stub(object, "method", func);.
我怎样才能替换像var stub = sinon.stub(object, "method", func);.
The main reason I am doing this is want to achieve multiple callback workaround as this mentioned
我这样做的主要原因是想要实现如上所述的多个回调解决方法
回答by g00glen00b
I don't think this does make a lot of sense, because if you're replacing a stub with another stub... then why don't you just use the first stub to achieve whatever you want to do with the second stub.
我认为这没有多大意义,因为如果您要用另一个存根替换一个存根……那么为什么不使用第一个存根来实现您想要用第二个存根做的任何事情。
Anyways, the sinon.stub(object, "method", func)does the following according to the docs:
无论如何,sinon.stub(object, "method", func)根据文档执行以下操作:
Replaces
object.methodwith afunc, wrapped in a spy. As usual,object.method.restore(); can be used to restore the original method.
替换
object.method为func, 包裹在间谍中。与往常一样,object.method.restore(); 可以用恢复原来的方法。
So, if you want to replace a stub function of a stub instance, then why don't you do:
所以,如果你想替换一个存根实例的存根函数,那么你为什么不这样做:
var a = sinon.createStubInstance(MyConstructor);
a.method = sinon.spy(function() { return "Foo Bar"; });
Or if you want to create a stub in stead of a spy:
或者,如果您想创建存根而不是间谍:
var a = sinon.createStubInstance(MyConstructor);
a.method = sinon.stub();
回答by flungo
After stubbing a whole object using sinon.createStubInstance(MyConstructor)or sinon.stub(obj)you can only replace the stub by either assigning a new stub to the property (as described by @g00glen00b) or restoring the stub before re-stubbing.
在使用sinon.createStubInstance(MyConstructor)or存根整个对象后,sinon.stub(obj)您只能通过将新存根分配给属性(如@g00glen00b 所述)或在重新存根之前恢复存根来替换存根。
var a = sinon.createStubInstance(MyConstructor);
a.method.restore();
sinon.stub(object, "method", func);
The advantage of this is that you can still call a.method.restore()afterwards with the expected behavior.
这样做的好处是您仍然可以在a.method.restore()之后以预期的行为进行调用。
It would be more convenient if the Stub API had a .call(func)method to override the function being called by the stub after the fact.
如果 Stub API 有一种.call(func)方法可以在事后覆盖由 stub 调用的函数,那就更方便了。
回答by Jan Molak
There's no need to overwrite the a.method, instead we can use callsFakeon the a.methoddirectly:
不需要覆盖a.method,而是我们可以直接callsFake在 上使用a.method:
const a = sinon.createStubInstance(MyContructor);
a.method.callsFake(func);
回答by Srini
1 more way to stub the method from 2.0 +
从 2.0 + 存根方法的另一种方法
sinon.stub(object, "method").callsFake(func);
object.method.restore()

