我用的是这个用于ES6的填充承诺和摩卡/柴。
我对这些承诺的断言是行不通的。以下是一个样本测试:
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);
});
});当我运行这个测试时,由于超时,它失败了。然后块中抛出的断言失败在catch块中被捕获。我怎么才能避免这件事直接扔到摩卡呢?
我可以将它从catch函数中抛出,但是接下来如何为catch块做出断言呢?
发布于 2015-03-08 03:36:48
最后,我通过使用柴某解决了我的问题。
它允许您对承诺的决议和拒绝作出断言:
return promise.should.become(value)return promise.should.be.rejected发布于 2015-03-02 05:47:23
如果你的承诺失败了,它只会调用你的catch回调。因此,Mocha的完成回调永远不会被调用,Mocha永远不会发现承诺失败(所以它等待并最终超时)。
您应该将console.log(err);替换为done(err);。当您将错误传递给已完成的回调时,Mocha应自动显示错误消息。
发布于 2015-04-03 18:22:17
我在Mocha/Chai/ES6测试中使用的一种模式是:
it('should do something', function () {
aPromiseReturningMethod(arg1, arg2)
.then(function (response) {
expect(response.someField).to.equal("Some value")
})
.then(function () {
return anotherPromiseReturningMethod(arg1, arg2)
})
.then(function (response) {
expect(response.something).to.equal("something")
})
.then(done).catch(done)
})最后一行看起来很奇怪,但称Mocha是成功的还是错误的。
一个问题是,如果最后一个返回什么,那么我需要在then和catch之前都使用noop()*
it('should do something', function () {
aPromiseReturningMethod(arg1, arg2)
.then(function (response) {
expect(response.someField).to.equal("Some value")
})
.then(function () {
return anotherPromiseReturningMethod(arg1, arg2)
})
.then(_.noop).then(done).catch(done)
})*Lodash's noop()
想听听对这种模式的任何批评。
https://stackoverflow.com/questions/28802603
复制相似问题