【发布时间】:2015-07-24 08:44:50
【问题描述】:
我想测试function B 以捕获从function A 与Mocha/Chai 引发的异常。
function A() {
// 1. the third party API is called here
// some exception may be thrown from it
...
// 2. some exception could be thrown here
// caused by the logic in this function
}
function B() {
// To catch exception thrown by A()
try {
A();
} catch(err) {
console.error(err);
}
...
}
我想强制A 抛出异常,同时对B 进行测试。所以我可以确保函数B 正确捕获来自A 的异常。
搜索了一些帖子后:
Test for expected failure in Mocha
Testing JS exceptions with Mocha/Chai
我没有找到正确的答案。
我的问题合理吗?如果是,如何使用Mocha/Chai 进行该测试?
【问题讨论】:
-
你不能只在你的 A 函数中
throw 'error';吗?
标签: javascript unit-testing mocha.js chai