我正在为 node.js 代码编写一些单元测试,我使用 Sinon 通过 stub 函数调用
var myFunction = sinon.stub(nodeModule, 'myFunction');
myFunction.returns('mock answer');
nodeModule
看起来像这样
module.exports = {
myFunction: myFunction,
anotherF: anotherF
}
function myFunction() {
}
function anotherF() {
myFunction();
}
模拟显然适用于像 nodeModule.myFunction()
这样的用例,但我想知道当使用 nodeModule.anotherF( )
?
最佳答案
您可以稍微重构一下您的模块。像这样。
var service = {
myFunction: myFunction,
anotherFunction: anotherFunction
}
module.exports = service;
function myFunction(){};
function anotherFunction() {
service.myFunction(); //calls whatever there is right now
}
关于node.js - Sinon 函数 stub : How to call "own" function inside module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35162659/