柴+摩卡,如果解决,则测试成功,如果拒绝,则测试失败

时间:2019-06-11 07:32:20

标签: unit-testing promise mocha chai chai-as-promised

我有一个返回承诺的函数。在我使用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)

1 个答案:

答案 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;
});