SINON存根更换功能不起作用

人气:896 发布:2022-10-16 标签: node.js sinon sinon-chai

问题描述

我在这里的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一个模块时的常见行为。更多详细信息: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                 
----------|---------|----------|---------|---------|-------------------

552