【问题标题】:How to check promise value and property updated in promise but not returned with chai-as-promised如何检查 Promise 值和属性在 Promise 中更新但未使用 chai-as-promised 返回
【发布时间】:2018-02-20 20:59:44
【问题描述】:

我有一个承诺,它会检查我是否被授权(返回真或假)。 在该承诺中,当授权为假时,我还将 403 statusCode 添加到我作为参数传递的响应对象中。 我正在用 chai-as-promised 测试我的承诺的结果,但我没有设法找到一种方法来在承诺解决后也测试响应的状态代码。

var result = authorizePromise(request, response);
return expect(result).to.eventually.equal(false).and(response.statusCode.should.be(403));

【问题讨论】:

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


    【解决方案1】:

    响应应在父范围内使用。
    并且应该在承诺准备好后调用断言
    所以它应该是一个在 之后调用的函数。
    您的代码示例 .and(response.statusCode.should.be(403)); 运行断言瞬间,并将其作为参数传递给 .and() 它不会等待承诺。

    *您也可以使用.and而不是.then

    var response = {};
    var resultPromise = authorizePromise(request, response);
    return expect(resultPromise).to.eventually.equal(false)
        .then(function(){return response.statusCode.should.be(403)})
    

    【讨论】:

    • 谢谢!在检查结果时,我没有考虑使用 .then 来确保我的承诺得到解决。
    猜你喜欢
    • 2014-07-22
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    相关资源
    最近更新 更多