【问题标题】:How to test the 'catch(e)' block in Mocha/Chai testing?如何在 Mocha/Chai 测试中测试“catch(e)”块?
【发布时间】:2020-05-11 02:29:39
【问题描述】:

我目前正在尝试覆盖此文件中的所有代码行,但在覆盖我的代码的 catch 块中的行时遇到问题。我已经编写了测试文件来涵盖其他所有内容,我将如何编写单元测试来涵盖 catch 块。

【问题讨论】:

  • 您是否故意删除了代码?如果没有,请恢复它,因为这个问题现在无法回答。

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


【解决方案1】:

您可以使用proxyquire 模拟oAuthStarGate 函数并在您的某些测试用例调用时抛出Èrror:

member-service.test.js:

const proxyquire = require('proxyquire').noCallThru();

...

const memberService = proxyquire('./member-service', {
  './oAuthStarGate': (updatedState, event) => {
    throw new Error('Error thrown');
  },
});

...

assert.throws(memberService(...), Error, 'Error thrown');

【讨论】:

    猜你喜欢
    • 2020-10-11
    • 1970-01-01
    • 2019-02-04
    • 2017-02-04
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    相关资源
    最近更新 更多