【问题标题】:How to test `catch` and `then` with sinon js?如何用 sinon js 测试 `catch` 和 `then`?
【发布时间】:2016-08-09 09:01:25
【问题描述】:

我一直在尝试存根和模拟我的函数以便能够测试我的函数

SetExample.createCandyBox = function(config) {
    this.getCandies(config.candyUrl)
    .catch(() => {
        return {};
    })
    .then((candies) => {
        Advertisements.pushCandyBox(config, candies);
    });
};

我想在 config.candyUrl 不正确(404 等)时测试一个场景,例如:

it('should return to an empty array when url is incorrect', sinon.test(function() {
    // Fixture is an element I created for testing. 
    var configStub = SetExample.getCandyConfig(fixture);
    var createCandyBoxMock = sinon.mock(config);

    createCandyBoxMock.expects('catch');

    SetExample. createCandyBox(configStub);
}));

当我这样做时,术语是错误 => 找不到变量:config. 我做错什么了?有人可以帮忙解释一下吗?我是Sinon的新手:(提前谢谢!

【问题讨论】:

  • 变量config 从未在您的测试中定义。你为什么期望它在那里,你期望它有什么价值?
  • 啊抱歉,我用SetExample 替换了config,它适用于我的简单测试函数return "yay!"。但是它仍然没有解决我的测试问题。我只想测试当我在createCandyBox 中输入错误的 URL 然后它返回到{}

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


【解决方案1】:

您可以使用sinon.stub() 存根this.getCandies 及其解析/拒绝值。并存根Advertisements.pushCandyBox,然后进行断言检查它是否已被调用。

例如

SetExample.js:

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

function SetExample() {}

SetExample.getCandies = async function (url) {
  return 'your real getCandies implementation';
};

SetExample.createCandyBox = function (config) {
  return this.getCandies(config.candyUrl)
    .catch(() => {
      return {};
    })
    .then((candies) => {
      Advertisements.pushCandyBox(config, candies);
    });
};

module.exports = SetExample;

Advertisements.js:

function Advertisements() {}

Advertisements.pushCandyBox = function (config, candies) {
  return 'your real pushCandyBox implementation';
};

module.exports = Advertisements;

SetExample.test.js

const SetExample = require('./SetExample');
const Advertisements = require('./Advertisements');
const sinon = require('sinon');

describe('38846337', () => {
  describe('#createCandyBox', () => {
    afterEach(() => {
      sinon.restore();
    });
    it('should handle error', async () => {
      const err = new Error('timeout');
      sinon.stub(SetExample, 'getCandies').withArgs('wrong url').rejects(err);
      sinon.stub(Advertisements, 'pushCandyBox');
      await SetExample.createCandyBox({ candyUrl: 'wrong url' });
      sinon.assert.calledWithExactly(SetExample.getCandies, 'wrong url');
      sinon.assert.calledWithExactly(Advertisements.pushCandyBox, { candyUrl: 'wrong url' }, {});
    });
  });
});

单元测试结果:

  38846337
    #createCandyBox
      ✓ should handle error


  1 passing (7ms)

-------------------|---------|----------|---------|---------|-------------------
File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-------------------|---------|----------|---------|---------|-------------------
All files          |   81.82 |      100 |   42.86 |   81.82 |                   
 Advertisements.js |   66.67 |      100 |       0 |   66.67 | 4                 
 SetExample.js     |    87.5 |      100 |      60 |    87.5 | 6                 
-------------------|---------|----------|---------|---------|-------------------

【讨论】:

    猜你喜欢
    • 2014-07-31
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2018-04-23
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    相关资源
    最近更新 更多