【发布时间】:2015-05-02 09:08:03
【问题描述】:
我正在使用this polyfill for ES6 promises 和 Mocha / Chai。
我对承诺的断言不起作用。下面是一个样例测试:
it('should fail', function(done) {
new Promise(function(resolve, reject) {
resolve(false);
}).then(function(result) {
assert.equal(result, true);
done();
}).catch(function(err) {
console.log(err);
});
});
当我运行此测试时,由于超时而失败。在 then 块中抛出的断言失败在 catch 块中被捕获。我怎样才能避免这种情况,直接把它扔给摩卡呢?
我可以将它从 catch 函数中抛出,但是我将如何为 catch 块进行断言呢?
【问题讨论】:
-
你检查this了吗?
-
不应该是
console.log(err);done(err);吗? -
@thefourtheye 承诺断言是我正在寻找的。谢谢,这是一个很好的资源。
-
@mido22 然后问题是对承诺的拒绝做出断言。但是,正如 thefourtheye 上面所说,Mocha 显然内置了这个功能。
-
如果你断言承诺成功并且你不需要断言任何其他东西,你可以直接返回承诺值而不调用
.then()或.catch()。这能回答你的问题吗?
标签: javascript mocha.js ecmascript-6 chai es6-promise