als*_*kja 7 unit-testing node.js sinon
我正在为node.js代码编写一些单元测试,我使用Sinon来存根函数调用
var myFunction = sinon.stub(nodeModule, 'myFunction');
myFunction.returns('mock answer');
Run Code Online (Sandbox Code Playgroud)
该nodeModule
是这样的
module.exports = {
myFunction: myFunction,
anotherF: anotherF
}
function myFunction() {
}
function anotherF() {
myFunction();
}
Run Code Online (Sandbox Code Playgroud)
nodeModule.myFunction()
模拟工作显然适用于类似的用例,但我想知道如何在调用时模拟另一个F()内的myFunction()调用nodeModule.anotherF()
?
Yur*_*nko 10
你可以稍微重构一下你的模块.像这样.
var service = {
myFunction: myFunction,
anotherFunction: anotherFunction
}
module.expors = service;
function myFunction(){};
function anotherFunction() {
service.myFunction(); //calls whatever there is right now
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2665 次 |
最近记录: |