【发布时间】:2015-12-29 20:15:31
【问题描述】:
我有一个可能会引发错误的方法,但是我在为这种情况编写一个 SinonJS/Mocha/Should 单元测试用例时遇到了麻烦。
正在测试的示例函数:
function testError(value) {
if (!value) {
throw new Error('No value');
return false;
}
};
样本测试:
describe('#testError', function() {
it('throws an error', function() {
var spy = sinon.spy(testError);
testError(false);
spy.threw().should.be.true();
});
});
这个输出:
#testError
1) throws an error
0 passing (11ms)
1 failing
1) #testError throws an error:
Error: No value
at testError (tests/unit/js/test-error.js:6:14)
at Context.<anonymous> (tests/unit/js/test-error.js:14:6)
我原以为诗浓会捕捉到错误并允许我监视投掷,但它似乎反而未能通过测试。有什么想法吗?
我提到了Don't sinon.js spys catch errors?,但唯一的解决方案是使用expect。如果可能的话,我更愿意使用一个断言库。
【问题讨论】:
标签: node.js unit-testing mocha.js sinon should.js