所以基本上我有一个函数,只有当参数等于某个值时,我才想对其行为进行 stub 。
例子
var sinon = require('sinon');
var foo = {
bar: function(arg1){
return true;
}
};
var barStub = sinon.stub(foo, "bar");
barStub.withArgs("test").returns("Hi");
// Expectations
console.log(foo.bar("test")); //works great as it logs "Hi"
// my expectation is to call the original function in all cases except
// when the arg is "test"
console.log(foo.bar("woo")); //doesnt work as it logs undefined
我正在使用这个包https://www.npmjs.com/package/sinon
最佳答案
我有同样的问题。接受的答案已过时。stub.callThrough();
将实现这一点。
只需调用barStub.callThrough()
在 barStub.withArgs("test").returns("Hi")
之后
https://sinonjs.org/releases/v7.5.0/stubs/
关于javascript - 只为一个参数 stub 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31575215/