【问题标题】:Mocking runtime config value with sinon使用 sinon 模拟运行时配置值
【发布时间】:2020-03-19 23:22:00
【问题描述】:

我在 hapi 服务器启动之前添加了一些配置值。应用程序工作正常,虽然在测试中我不能使用 config.get()。我可以使用proxyquire。所以我想知道

  • “动态”添加配置文件是不好的设计吗?
  • 有没有办法在这种情况下使用 config.get()?
  • 任何替代方法?

    //initialize.js
    
    const config = require('config');
    
    async function startServe() {
      const someConfigVal = await callAPIToGetSomeJSONObject();
      config.dynamicValue = someConfigVal;
      server.start(); 
    }
    
    //doSomething.js
    
    const config = require('config');
    
    function doesWork() {
      const valFromConfig = config.dynamicValue.X;
      // In test I can use proxiquire by creating config object
      ...
    }
    
    function doesNotWork() {
      const valFromConfig = config.get('dynamicValue.X'); 
      // Does not work with sinon mocking as this value does not exist in config when test run.
      // sinon.stub(config, 'get').withArgs('dynamicValue.X').returns(someVal);
      .....
    }
    

【问题讨论】:

    标签: config sinon proxyquire


    【解决方案1】:

    上下文:测试。

    • “动态”添加配置文件是不好的设计吗? => 不,我以前做过。测试代码在测试中更改配置文件:default.json 以检查被测函数是否按预期运行。我用了几个config utilities
    • 有什么方法可以在这种情况下使用 config.get() 吗? => 是的。关于 sinon 的用法,请参见下面的示例,它使用 mocha。您需要在被测函数使用它之前定义存根/模拟,并且不要忘记恢复存根/模拟。还有与此相关的官方文档:Altering configuration values for testing at runtime,但没有使用 sinon。
    const config = require('config');
    const sinon = require('sinon');
    const { expect } = require('chai');
    
    // Example: simple function under test.
    function other() {
      const valFromConfig = config.get('dynamicValue.X');
      return valFromConfig;
    }
    
    describe('Config', function () {
      it ('without stub or mock.', function () {
        // Config dynamicValue.X is not exist.
        // Expect to throw error.
        try {
          other();
          expect.fail('expect never get here');
        } catch (error) {
          expect(error.message).to.equal('Configuration property "dynamicValue.X" is not defined');
        }
      });
    
      it('get using stub.', function () {
        // Create stub.
        const stubConfigGet = sinon.stub(config, 'get');
        stubConfigGet.withArgs('dynamicValue.X').returns(false);
        // Call get.
        const test = other();
        // Validate te result.
        expect(test).to.equal(false);
        expect(stubConfigGet.calledOnce).to.equal(true);
        // Restore stub.
        stubConfigGet.restore();
      });
    
      it('get using mock.', function () {
        // Create mock.
        const mockConfig = sinon.mock(config);
        mockConfig.expects('get').once().withArgs('dynamicValue.X').returns(false);
        // Call get.
        const test = other();
        // Validate te result.
        expect(test).to.equal(false);
        // Restore mock.
        expect(mockConfig.verify()).to.equal(true);
      });
    });
    
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 2015-11-10
      • 2017-07-25
      • 1970-01-01
      相关资源
      最近更新 更多