【问题标题】:How do I unit test using sinon?如何使用 sinon 进行单元测试?
【发布时间】:2021-03-15 12:12:08
【问题描述】:

A.js

const func2 = () => 'world';

module.exports = {func2}

Util.js

const {func2} = require("./A");

const func1 = () => {
    return 'hello ' + func2();  // <= use the module
  }

  module.exports = { func1 }

 

Util.test.js

const sinon = require('sinon');
const {func1} = require('./util');
const a = require('./A');
const chai = require("chai");
const expect = chai.expect;

describe('func1', () => {
  it('should work', () => {
    const stub = sinon.stub(a, 'func2').returns('everyone');
    expect(func1()).to.be.equal('hello everyone');  // Success!
  });
});

获取断言失败... Sinon stub func2 没有存根

AssertionError: 预期 'hello world' 等于 'hello 大家' 在上下文。 (测试\util.test.js:10:27) 在 processImmediate (internal/timers.js:456:21)

  • 预期 - 实际

-“你好世界” +“大家好”

【问题讨论】:

  • func2 不能像那样被存根,因为 func1 已经需要原始的 func2 而不是你之后创建的存根,所以断言合理地失败了
  • 那我就没有办法测试这个功能了吗?
  • 因为它现在编码不,但是如果你使用 func2 作为依赖而不是按原样需要它,有一种方法,称为依赖注入
  • 非常感谢..明白了..

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


【解决方案1】:

如果您仍然想要它:将 Util.js 中的实现更改为类似的内容。

const a = require("./a");

const func1 = () => {
  return 'hello ' + a.func2();
}

module.exports = { func1 };

证明:

$ npx mocha util.test.js 


  func1
    ✓ should work


  1 passing (3ms)

注意:它仍然不是依赖注入,但调用 func2 的方式变化不大。

【讨论】:

    【解决方案2】:

    您可以将func2 与Link Seams 分开。这是 CommonJS 版本,所以我们将使用proxyquire 来构造我们的接缝。

    例如

    a.js:

    const func2 = () => 'world';
    
    module.exports = { func2 };
    

    util.js:

    const { func2 } = require('./a');
    
    const func1 = () => {
      return 'hello ' + func2();
    };
    
    module.exports = { func1 };
    

    util.test.js:

    const sinon = require('sinon');
    const chai = require('chai');
    const proxyquire = require('proxyquire');
    const expect = chai.expect;
    
    describe('func1', () => {
      it('should work', () => {
        const func2Stub = sinon.stub().returns('everyone');
        const { func1 } = proxyquire('./util.js', {
          './a': {
            func2: func2Stub,
          },
        });
        expect(func1()).to.be.equal('hello everyone');
      });
    });
    

    单元测试结果:

      func1
        ✓ should work (1794ms)
    
    
      1 passing (2s)
    
    ----------|---------|----------|---------|---------|-------------------
    File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ----------|---------|----------|---------|---------|-------------------
    All files |   85.71 |      100 |      50 |     100 |                   
     a.js     |   66.67 |      100 |       0 |     100 |                   
     util.js  |     100 |      100 |     100 |     100 |                   
    ----------|---------|----------|---------|---------|-------------------
    

    【讨论】:

      猜你喜欢
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 2023-04-01
      相关资源
      最近更新 更多