我們都知道,當我回報摩卡的承諾時,它會測試承諾。如何在摩卡中測試承諾
但是,如果它在承諾中拋出異常,如何判斷異常是否拋出。也就是說,如果異常是有保證的,那麼測試通過。
it("Should not take game code for the same user twice", function (done) {
return gameGiftManage.takeGameCode(gameGiftId, userId)
.catch(function (e) {
expect(e).to.exist;
done();
})
})
這是我用來測試異常的測試,但它在某些情況下不起作用。
takeGameCode:
takeGameCode: function (giftId, userId) {
return GameGiftCode.count({gift: giftId, user: userId}).exec().then(function (c) {
if (c) {
throw '該用戶已經領取過禮包';
}
}).then(function() {
return GameGiftCode.findOneAndUpdate({gift: giftId, user: {$exists: false}}, {user: userId}).exec();
}).then(function (a) {
if (!a) {
throw '禮包領完了';
} else {
return a;
}
});
},
在你的承諾中..你使用throw()還是reject()? –
@JamesLeClair扔 –