【问题标题】:Proxyquire not overriding exported functionProxyquire 不覆盖导出的函数
【发布时间】:2021-12-13 23:57:17
【问题描述】:

我有一个班级modules/handler.js,看起来像这样:

const {getCompany} = require('./helper');

module.exports = class Handler {
    constructor () {...}
    async init(){
        await getCompany(){
        ...
    }
}

它从文件modules/helper.js中导入函数getCompany

exports.getCompany = async () => {
 // async calls
}

现在在集成测试中,我想存根 getCompany 方法,它应该只返回一个 mockCompany。 但是,proxyquire 并没有对方法getCompany 进行存根,而是调用真正的方法。 测试:

const sinon = require('sinon');
const proxyquire = require("proxyquire");
const Handler = require('../modules/handler');

describe('...', () => {

    const getCompanyStub = sinon.stub();
    getCompanyStub.resolves({...});

    const test = proxyquire('../modules/handler.js'), {
      getCompany: getCompanyStub
    });

    it('...', async () => {
        const handler = new Handler();
        await handler.init(); // <- calls real method 
        ... 
    });
});

我也尝试过不使用sinon.stub,其中 proxyquire 返回一个函数,直接返回一个对象,但是,这也不起作用。

我会非常感谢每一个指针。 谢谢。

【问题讨论】:

    标签: node.js testing mocking sinon proxyquire


    【解决方案1】:

    require 函数需要您使用的 Handler 类,而不是 proxyquire

    handler.js:

    const { getCompany } = require('./helper');
    
    module.exports = class Handler {
      async init() {
        await getCompany();
      }
    };
    

    helper.js:

    exports.getCompany = async () => {
      // async calls
    };
    

    handler.test.js:

    const sinon = require('sinon');
    const proxyquire = require('proxyquire');
    
    describe('69759888', () => {
      it('should pass', async () => {
        const getCompanyStub = sinon.stub().resolves({});
        const Handler = proxyquire('./handler', {
          './helper': {
            getCompany: getCompanyStub,
          },
        });
        const handler = new Handler();
        await handler.init();
      });
    });
    

    测试结果:

      69759888
        ✓ should pass (2478ms)
    
    
      1 passing (2s)
    
    ------------|---------|----------|---------|---------|-------------------
    File        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ------------|---------|----------|---------|---------|-------------------
    All files   |     100 |      100 |     100 |     100 |                   
     handler.js |     100 |      100 |     100 |     100 |                   
     helper.js  |     100 |      100 |     100 |     100 |                   
    ------------|---------|----------|---------|---------|-------------------
    

    【讨论】:

    • 您好,感谢您的回复!感谢您,我能够查明问题所在。虽然使用 const Handler = proxyquire('../modules/handler', { getCompany: getCompanyStub, });对我不起作用,但 const Handler = proxyquire('../modules/handler', { './helpers' : { getCompany: getCompanyStub},});工作。这对我来说似乎很奇怪,但我会接受的。
    • @seveneights 抱歉,我有一个错字。更新了答案。你是对的
    猜你喜欢
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 2010-10-25
    • 2021-03-14
    • 2017-04-09
    • 1970-01-01
    相关资源
    最近更新 更多