【问题标题】:Spy module.exports functions with sinon in NodeJS always returning 0在 NodeJS 中使用 sinon 的 Spy module.exports 函数总是返回 0
【发布时间】:2021-08-27 16:21:00
【问题描述】:

我正在尝试使用用 Sinon 编写的测试用例来跟踪对另一个函数的调用次数。我有以下代码,在运行测试时,calledFuncSpy callCount 总是让我得到 0,这是意料之外的。同时,在console.log 上运行另一个间谍会给我正确的输出,我在代码中留下了注释。有谁知道可能是什么问题?

这是代码。如果需要更多信息来解决此问题,请告诉我。

specs/mymodule.js

const mymodule = require("../src/mymodule.js");
var sinon = require("sinon");
const assert = require("assert");



describe("Test suite", function() {
  let calledFuncSpy;
  let consoleSpy;

  beforeEach(function () {
    calledFuncSpy = sinon.spy(mymodule, "calledFunc");
    consoleSpy = sinon.spy(console, "log");
  })
  afterEach(function () {
    calledFuncSpy.restore();
    consoleSpy.restore();
  })

  it("Test the add method", function () {
    mymodule.ownHelloWorld();
    assert.deepStrictEqual(calledFuncSpy.callCount, 1);
    // assert.deepStrictEqual(consoleSpy.callCount, 1);
  });
})

src/mymodule.js

function ownHelloWorld() {
  calledFunc();
}

function calledFunc() {
  console.log("Hello world");
}

module.exports = {
  ownHelloWorld,
  calledFunc
}

【问题讨论】:

    标签: node.js unit-testing testing sinon


    【解决方案1】:

    calledFuncownHelloWorld 函数内调用不能被存根。它将始终使用原始的calledFunc 函数。 sinon.spy(mymodule, "calledFunc") 在调用ownHelloWorld 函数时不能修改它的引用。另外,请参阅comment

    你可以这样重构它:

    mymodule.js:

    function ownHelloWorld() {
      module.exports.calledFunc();
    }
    
    function calledFunc() {
      console.log('Hello world');
    }
    
    module.exports = {
      ownHelloWorld,
      calledFunc,
    };
    

    mymodule.test.js:

    const mymodule = require('./mymodule');
    var sinon = require('sinon');
    
    describe('Test suite', function () {
      let calledFuncSpy;
      let consoleSpy;
    
      beforeEach(function () {
        calledFuncSpy = sinon.spy(mymodule, 'calledFunc');
        consoleSpy = sinon.spy(console, 'log');
      });
      afterEach(function () {
        calledFuncSpy.restore();
        consoleSpy.restore();
      });
    
      it('Test the add method', function () {
        mymodule.ownHelloWorld();
        sinon.assert.calledOnce(calledFuncSpy);
      });
    });
    

    测试结果:

      Test suite
    Hello world
        ✓ Test the add method
    
    
      1 passing (5ms)
    
    -------------|---------|----------|---------|---------|-------------------
    File         | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    -------------|---------|----------|---------|---------|-------------------
    All files    |     100 |      100 |     100 |     100 |                   
     mymodule.js |     100 |      100 |     100 |     100 |                   
    -------------|---------|----------|---------|---------|-------------------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 2019-05-07
      • 2011-08-27
      • 2017-04-21
      • 2018-09-26
      相关资源
      最近更新 更多