【问题标题】:How to stub a required function in Sinon?如何在 Sinon 中存根所需的函数?
【发布时间】:2021-10-21 20:24:13
【问题描述】:

我正在尝试在我的文件中存根所需的函数。例如:

fileA.js

function doSomething() {
  return 5;
}

modules.export = { doSomething }

fileB.js

const { doSomething } = require('./fileA');

function doAnotherThing() {
  let anotherThing = 10;
  return anotherThing + doSomething();
}

modules.export = { doAnotherThing }

在我的测试文件中:fileBTest.js

const fileA = require('./fileA');
const fileB = require('./fileB');

describe('Test File B', function(){
  it('Example of failing stub', function() {
    const fileAStub = sinon.stub(fileA, 'doSomething');
    fileAStub.returns(15);

    expect(fileB.doAnotherThing()).to.equal(25);
  })
})

但是,我的 fileAStub 无法正常工作,因为在 fileB.js 中,它是一个函数所必需的,而不是整个 fileA 所必需的。

如果我将 fileB.js 中的 fileA 要求更改为以下方式,我的存根将起作用。

updatedFileB.js

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

function doAnotherThing() {
  let anotherThing = 10;
  return anotherThing + fileA.doSomething();
}

问题:我如何存根 { doSomething } 而不是更改我在 fileB 中要求的方式?

【问题讨论】:

    标签: node.js unit-testing chai sinon


    【解决方案1】:

    来自How to stub a dependency of a module docs:

    为了使存根工作,存根方法不能被解构,无论是在被测模块还是在测试中。

    所以你不应该从fileB.js 中的模块fileA.js 中解构doSomething 函数。

    但是,如果你不想重构代码,你可以使用Link Seams 来做。

    例如

    fileA.js:

    function doSomething() {
      return 5;
    }
    
    module.exports = { doSomething };
    

    fileB.js:

    const { doSomething } = require('./fileA');
    
    function doAnotherThing() {
      let anotherThing = 10;
      return anotherThing + doSomething();
    }
    
    module.exports = { doAnotherThing };
    

    fileB.test.js:

    const sinon = require('sinon');
    const { expect } = require('chai');
    const proxyquire = require('proxyquire');
    
    describe('Test File B', function() {
      it('Example of failing stub', function() {
        const doSomethingStub = sinon.stub().returns(15);
        const fileB = proxyquire('./fileB', {
          './fileA': { doSomething: doSomethingStub },
        });
        expect(fileB.doAnotherThing()).to.equal(25);
        sinon.assert.calledOnce(doSomethingStub);
      });
    });
    

    测试结果:

      Test File B
        ✓ Example of failing stub
    
    
      1 passing (53ms)
    
    ----------|---------|----------|---------|---------|-------------------
    File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ----------|---------|----------|---------|---------|-------------------
    All files |   83.33 |      100 |      50 |   83.33 |                   
     fileA.js |      50 |      100 |       0 |      50 | 2                 
     fileB.js |     100 |      100 |     100 |     100 |                   
    ----------|---------|----------|---------|---------|-------------------
    

    【讨论】:

      猜你喜欢
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 1970-01-01
      相关资源
      最近更新 更多