Sinonfunction存根:如何在模块内调用“自带”function
我正在为node.js代码编写一些unit testing,并使用Sinon通过存根函数调用
var myFunction = sinon.stub(nodeModule, 'myFunction'); myFunction.returns('mock answer');
nodeModule
看起来像这样
module.exports = { myFunction: myFunction, anotherF: anotherF } function myFunction() { } function anotherF() { myFunction(); }
Mocking对于像nodeModule.myFunction()
这样的用例显然nodeModule.myFunction()
,但是我想知道如何在用nodeModule.anotherF()调用时如何在另一个F()内模拟myFunction()调用?
你可以稍微重构你的模块。 喜欢这个。
var service = { myFunction: myFunction, anotherFunction: anotherFunction } module.expors = service; function myFunction(){}; function anotherFunction() { service.myFunction(); //calls whatever there is right now }