【发布时间】:2014-07-22 01:32:28
【问题描述】:
我正在尝试使用Chai-as-Promised、Mocha 和“应该”方言来测试被拒绝的 Promise 的细节。 Promise 由bluebird 实现。
这很好用:
it('it should be rejected when given bad credentials', function () {
var promiseOfUsers = db.auth("bad", "credentials").getUsers();
return promiseOfUsers.should.eventually.be.rejectedWith(Error)
});
该错误有一个“状态”属性。我想断言状态是 401
这不起作用:
it('it should be rejected when given bad credentials', function () {
var promiseOfUsers = db.auth("bad", "credentials").getUsers();
return promiseOfUsers.should.eventually.be.rejectedWith(Error)
.that.has.property('status')
.that.equals(401)
});
似乎任何不引用“rejected”或rejectedWith(Error) 的断言尝试都会失败,并且只会将错误打印到控制台。
如何深入了解拒绝的原因?
【问题讨论】:
-
试试
should.eventually.be.rejectedWith(Error).and.eventually.have.property("status").that.equals(401) -
谢谢,但这并没有帮助。我将更新问题以显示结果。
-
嗯
.should.eventually.be.rejected.and.eventually.have.property("status").that.equals(401);有效,但不检查错误类型。 jsfiddle.net/F9SKD -
嗯。我个人只会做
.catch(function(err){ return err instanceof Error && error.status === 401}); -
与小修正“error.status”一起工作的应该是“err.status”
标签: javascript testing promise mocha.js chai