我正在尝试使用async.waterfall
对我的一个函数进行存根来测试Sinon.js。
// functions.js
module.exports = {
// function I don't want to run
doBigThing: function() {
console.log("[doBigThing] was called");
},
// function I want to stub
myFunction: function(number, callback) {
console.log("[myFunction] was called");
doBigThing();
callback(null, number);
},
// function I want to test
waterfall: function(callback) {
return async.waterfall([
async.constant(5), // 5 just for the demo
myFunction
], callback);
}
}
我的测试是:
describe('water', function() {
it ('successfully falls', function() {
// function under test
var waterfall = functions.waterfall;
var callback = function(err, number) {
expect(err).to.be.null;
expect(number).to.equal(5);
};
// I would like this stub to run instead of functions.myFunction
sinon.stub(functions, 'myFunction', function(number, callback) {
console.log("[myFunction] stub was called");
callback(null, number);
});
waterfall(callback);
// I suppose this is happening: myFunction(5, callback)
expect(functions.myFunction.withArgs(5, callback)).to.have.been.called;
expect(callback).to.have.been.called;
});
});
因此测试通过,但是存根被忽略,因为调用了doBigThing
:
Water
✓ successfully falls
[myFunction] was called
[doBigThing] was called
相反,我希望看到
Water
✓ successfully falls
[myFunction] stub was called
我可能错过了什么,我很感激你的帮助。
答案 0 :(得分:1)
您正在使用functions
对象的方法 myFunction
,但在waterfall
方法中,您正在调用{{} 1}} 功能(我实际上无法在我的环境中运行您的代码,我得到" ReferenceError:myFunction未定义")。所以这应该有效:
myFunction