我有一个返回承诺的函数。在我使用chai的测试文件中,我希望发生以下情况:
const result = sendSurveyDataToAnalytics(userId,eventType,eventTitle)
result.then(() => {
Logger.info("Succeed in the test if we get here")
}).catch(() => {
Logger.info("Fail in the test if we get here")
});
代码说明了这一点。成功后,失败。可能期望或应该这样做的正确方法是什么(已经安装了chai-promise)
答案 0 :(得分:1)
如果您使用的是chai-as-promised
:
const result = sendSurveyDataToAnalytics(userId, eventType, eventTitle);
result.then(() => {
Logger.info("Succeed in the test if we get here");
}).catch(() => {
Logger.info("Fail in the test if we get here");
});
it('resolves as promised', function() {
return result.should.be.fulfilled;
});
// or:
it('rejects as promised', function() {
return result.should.be.rejected;
});