如何对模块中导出的函数返回的对象进行存根

时间:2020-03-18 19:50:33

标签: javascript node.js unit-testing chai sinon

考虑到我有以下内容:

注意:下面显示的示例只是为了简洁起见,没有考虑实际的用例

文件:simpleCalcs.js

module.exports = function () {
    return {
        sum: (listOfNumbers) => {
            console.log('inside test2 sum()');
            return listOfNumbers.reduce((a, b) => a + b, 0);
        }
    }
};

文件:mediumCalcs.js

const simpleCalcs = require('./simpleCalcs')();

module.exports = function () {
    return {
        avg: (listOfNumbers) => {
            console.log('inside test1 avg()')
            return simpleCalcs.sum(listOfNumbers) / listOfNumbers.length;
        }
    }
};

文件:mediumCalcs.test.js

const sinon = require('sinon');
const expect = require('chai').expect;
const simpleCalcs = require('./simpleCalcs')();
const mediumCalcs = require('./mediumCalcs')();

describe('sample test', () => {

    before(() => {
        summationStub = sinon.stub(simpleCalcs, 'sum').returns(3);
    });

    it('average', () => {
        const result = mediumCalcs.avg([1, 2, 3]);
        expect(result).to.be.equal(1);
    });
});

此测试失败,因为未调用存根函数。

如何确保在存根过程中照顾module.exports = function () {....}

sinon能够满足此类要求吗?

P.S之所以使用module.exports = function () {....}而不是module.exports = {....}是因为可以通过参数注入某些依赖项。

1 个答案:

答案 0 :(得分:0)

您可以使用link seams,所以我将使用proxyquire对其进行存根。

例如

simpleCalcs.js

module.exports = function() {
  return {
    sum: (listOfNumbers) => {
      console.log('inside test2 sum()');
      return listOfNumbers.reduce((a, b) => a + b, 0);
    },
  };
};

mediumCalcs.js

const simpleCalcs = require('./simpleCalcs')();

module.exports = function() {
  return {
    avg: (listOfNumbers) => {
      console.log('inside test1 avg()');
      return simpleCalcs.sum(listOfNumbers) / listOfNumbers.length;
    },
  };
};

mediumCalcs.test.js

const sinon = require('sinon');
const expect = require('chai').expect;
const proxyquire = require('proxyquire');

describe('sample test', () => {
  it('average', () => {
    const simpleCalcsStub = {
      sum: sinon.stub().returns(3),
    };
    const simpleCalcs = sinon.stub().callsFake(() => simpleCalcsStub);
    const mediumCalcs = proxyquire('./mediumCalcs.js', {
      './simpleCalcs': simpleCalcs,
    })();
    const result = mediumCalcs.avg([1, 2, 3]);
    expect(result).to.be.equal(1);
    sinon.assert.calledOnce(simpleCalcs);
    sinon.assert.calledWithExactly(simpleCalcsStub.sum, [1, 2, 3]);
  });
});

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

  sample test
inside test1 avg()
    ✓ average (1613ms)


  1 passing (2s)

----------------|---------|----------|---------|---------|-------------------
File            | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------------|---------|----------|---------|---------|-------------------
All files       |      60 |      100 |      40 |   66.67 |                   
 mediumCalcs.js |     100 |      100 |     100 |     100 |                   
 simpleCalcs.js |      20 |      100 |       0 |      25 | 2,4,5             
----------------|---------|----------|---------|---------|-------------------

源代码:https://github.com/mrdulin/expressjs-research/tree/master/src/stackoverflow/60746613