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

Unit testing 柴+;摩卡,如果解决,测试成功,如果拒绝,测试失败,unit-testing,promise,mocha.js,chai,chai-as-promised,Unit Testing,Promise,Mocha.js,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 h

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

代码解释了这一点。成功在那时,失败在捕获。使用maybe expect或should(已按承诺安装chai)

的正确方法是什么?如果您按承诺使用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");
});

it('resolves as promised', function() {
    return result.should.be.fulfilled;
});

// or:
it('rejects as promised', function() {
    return result.should.be.rejected;
});