我使用 chai-as-promised 库和 q 库生成的承诺。这个简单的测试用例应该可以工作(必须拒绝承诺)还是我误解了承诺功能?
bdd.it("Test rejection", function () {
var promise = q.promise(function (resolve, reject, notify) {
reject(new Error("test"));
}).then(function () {
// Nothing to do
});
promise.should.be.rejectedWith(Error);
return promise;
});
此测试失败并显示错误:测试(我使用 Intern 作为单元测试库)虽然以下测试通过:
bdd.it("Test rejection", function () {
var promise = q.promise(function (resolve, reject, notify) {
reject(new Error("test"));
}).should.be.rejectedWith(Error);
return promise;
});
该库需要您返回 的返回值,.rejectedWith()
以便它测试断言。您只是.should.be.rejectedWith()
在测试过程中调用,它对此无能为力。
如果您查看chai-as-promised的文档,您会发现这正是他们在示例中所做的:
return promise.should.be.rejectedWith(Error);
对于其他基于 Promise 的断言,如.should.become()
.
你的第二次测试是正确的。您也可以只使用return
而不是先将结果分配给变量:
bdd.it("Test rejection", function () {
return q.promise(function (resolve, reject, notify) {
reject(new Error("test"));
}).should.be.rejectedWith(Error);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句