【问题标题】:Chai + mocha, succeed in test if resolved, fail if rejectedChai + mocha,解决则测试成功,拒绝则失败
【发布时间】:2019-10-25 13:52:13
【问题描述】:

我有一个返回承诺的函数。在我使用 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-as-promised)的正确方法是什么

【问题讨论】:

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


    【解决方案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;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-28
      • 2019-10-25
      • 2021-04-24
      • 2019-07-18
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      相关资源
      最近更新 更多