Gya*_*eep 4 javascript mocking node.js sinon
所以基本上我有一个函数,仅当参数等于某个值时我才想对其行为进行存根。例子
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
Run Code Online (Sandbox Code Playgroud)
小智 8
我有同样的问题。接受的答案已过时。
stub.callThrough();将实现这一目标。
barStub.callThrough()稍后打电话barStub.withArgs("test").returns("Hi")
https://sinonjs.org/releases/v7.5.0/stubs/
| 归档时间: |
|
| 查看次数: |
2010 次 |
| 最近记录: |