我正在尝试在摩卡咖啡中测试失败。我想在此请求成功时注册失败,因为它不应该。我的问题是,当我运行assert(false)
时,它似乎会触发catch
。
it('Should fail to complete this hail, because driver is not driver', (done) => {
req(rider, '/hail/complete', {
id: driver.id
}).then(() => {
assert(false);
done();
}).catch((err) => {
assert.equal(1, err.error.errors.length);
done();
});
});
最佳答案
这就是承诺链的工作方式:如果.then()
引发异常,则随后的.catch()
会捕获该异常。
由于要捕获req()
引发的拒绝,因此可以通过在.then()
上添加拒绝处理程序来解决此问题。而且,由于您使用的是Mocha,因此可以利用Mocha支持promise的事实。
所有这些将使您能够执行此操作:
it('Should fail to complete this hail, because driver is not driver', () => {
return req(rider, '/hail/complete', { id: driver.id }).then(() => {
assert(false);
}, err => {
assert.equal(1, err.error.errors.length);
});
});
关于javascript - 测试应该在 Mocha 中失败的东西。 (断言不接),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38437598/