"use strict";
let assert = require("assert");
describe("Promise test", function() {
it('should pass', function(done) {
var a = {};
var b = {};
a.key = 124;
b.key = 567;
let p = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve();
}, 100)
});
p.then(function success() {
console.log("success---->", a, b);
assert.deepEqual(a, b, "response doesnot match");
done();
}, function error() {
console.log("error---->", a, b);
assert.deepEqual(a, b, "response doesnot match");
done();
});
});
});输出:

我使用的是节点v5.6.0。当值不匹配时,测试似乎要等待断言。
我试着检查assert.deepEqual是否存在使用setTimeout的问题,但效果很好。
但它在使用诺言时失败,如果值不匹配,则挂起。
发布于 2016-04-14 09:59:27
你会得到那个错误,因为你的测试永远不会结束。这个断言:assert.deepEqual(a, b, "response doesnot match");抛出一个错误,因此由于您还没有捕获块,所以永远不会调用done回调。
您应该在承诺链的末尾添加catch块:
...
p.then(function success() {
console.log("success---->", a, b);
assert.deepEqual(a, b, "response doesnot match");
done();
}, function error() {
console.log("error---->", a, b);
assert.deepEqual(a, b, "response doesnot match");
done();
})
.catch(done); // <= it will be called if some of the asserts are failed发布于 2016-04-14 19:47:09
由于您使用的是承诺,我建议在it的末尾返回它。一旦承诺得到解决(履行或拒绝),Mocha将考虑所做的测试,并将消耗承诺。在拒绝承诺的情况下,它将使用其值作为抛出的错误。
注:如果您要返回承诺,请不要声明done参数。
https://stackoverflow.com/questions/36619090
复制相似问题