node.js - Sinon 函数 stub : How to call "own" function inside module

标签 node.js unit-testing sinon

我正在为 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/

相关文章:

node.js - 杀死所有子进程,但不杀死父进程

unit-testing - 在 Jest 测试覆盖率报告中显示 'E'

java - Selenium 2 等到 AJAX 在 JAVA 中完成

javascript - Sinon 似乎没有监视事件处理程序回调

node.js - Sinon stub 在 Node.js 和 mocha 测试中未定义

node.js - 一枪流

node.js - 函数返回一个空数组

python:测试子进程调用是否抛出预期异常

javascript - Sinon Stub 单元测试

mysql - 使用 mysql 和 node.js 更新 foreach 外部的变量值