g00*_*00b 15
我认为这没有多大意义,因为如果你用另一个存根替换存根...那你为什么不用第一个存根来实现你想用第二个存根做什么.
无论如何,sinon.stub(object, "method", func)根据文档做以下事项:
object.method用a 替换func,用间谍包裹.像往常一样object.method.restore(); 可用于恢复原始方法.
因此,如果要替换存根实例的存根函数,那么为什么不这样做:
var a = sinon.createStubInstance(MyConstructor);
a.method = sinon.spy(function() { return "Foo Bar"; });
Run Code Online (Sandbox Code Playgroud)
或者如果你想创建一个存根而不是间谍:
var a = sinon.createStubInstance(MyConstructor);
a.method = sinon.stub();
Run Code Online (Sandbox Code Playgroud)
flu*_*ngo 11
使用sinon.createStubInstance(MyConstructor)或使用或者sinon.stub(obj)只能通过为属性分配新存根(如@ g00glen00b所述)或在重新存根之前恢复存根来替换存根之后.
var a = sinon.createStubInstance(MyConstructor);
a.method.restore();
sinon.stub(object, "method", func);
Run Code Online (Sandbox Code Playgroud)
这样做的好处是,您仍然可以a.method.restore()使用预期的行为进行调用.
如果Stub API有一个.call(func)方法来覆盖事实后被存根调用的函数会更方便.