Sinon 存根替换功能不起作用

Sinon stub replacing function not working

我在这里孤立了我在 nodeJs 中面临的问题。 Sinon 对依赖函数的存根未按预期工作。我没有得到我在这里缺少的东西。感谢帮助。这是示例代码。

sinonTest.js

"use strict";
function getSecretNumber () {
  return 44;
}
function getTheSecret () {
  return `The secret was: ${getSecretNumber()}`;
}

module.exports = {
  getSecretNumber,
  getTheSecret,
};

sinonTest_spec.ts

"use strict";
const sinon = require("sinon");
const sinonMediator = require("./sinonTest");
const assert = require("assert");

describe("moduleUnderTest", function () {
  describe("when the secret is 3", function () {
    beforeEach(function () {
      sinon.stub(sinonMediator, "getSecretNumber").returns(3);
    });
    afterEach(function (done) {
      sinon.restore();
      done();
    });
    it("should be returned with a string prefix", function () {
      const result = sinonMediator.getTheSecret();
      const stubValue = sinonMediator.getSecretNumber();    
      assert.equal(stubValue, 3);                         //this assertion passed
      assert.equal(result, "The secret was: 3");          //but this assertion failed.
    });
  });
});

这是我在执行测试用例时遇到的断言错误。

AssertionError [ERR_ASSERTION]: 'The secret was: 44' == 'The secret was: 3'

谢谢。

这是当你 require 模块时的 commonjs 行为。更多详情:https://nodejs.org/docs/latest/api/modules.html#modules_exports_shortcut

function require(/* ... */) {
  const module = { exports: {} };

  ((module, exports) => {
    function getSecretNumber() {
      return 44;
    }
    function getTheSecret() {
      return `The secret was: ${getSecretNumber()}`;
    }
    module.exports = {
      getTheSecret,
      getSecretNumber,
    };
  })(module, module.exports);

  return module.exports;
}

你可以存根module.exports.getSecretNumber方法,但是在getTheSecret中调用的getSecretNumber函数仍然是原始函数,而不是存根函数。这就是为什么你的存根不起作用。

index.js:

'use strict';
function getSecretNumber() {
  return 44;
}
function getTheSecret() {
  return `The secret was: ${exports.getSecretNumber()}`;
}

exports.getSecretNumber = getSecretNumber;
exports.getTheSecret = getTheSecret;

index.test.js:

'use strict';
const sinon = require('sinon');
const sinonMediator = require('./');
const assert = require('assert');

describe('moduleUnderTest', function() {
  describe('when the secret is 3', function() {
    beforeEach(function() {
      sinon.stub(sinonMediator, 'getSecretNumber').returns(3);
    });
    afterEach(function(done) {
      sinon.restore();
      done();
    });
    it('should be returned with a string prefix', function() {
      const result = sinonMediator.getTheSecret();
      const stubValue = sinonMediator.getSecretNumber();
      assert.equal(stubValue, 3); 
      assert.equal(result, 'The secret was: 3');
    });
  });
});

带有覆盖率报告的单元测试结果:

  moduleUnderTest
    when the secret is 3
      ✓ should be returned with a string prefix


  1 passing (25ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |      75 |      100 |      50 |      75 |                   
 index.js |      75 |      100 |      50 |      75 | 3                 
----------|---------|----------|---------|---------|-------------------