我正在尝试使用异步 Jasmine Node :
it('should not be dumb', function(done){
Promise.resolve(0).then(function(){
console.log('dumb');
expect(2).toEqual(2);
done();
});
});
返回:
dumb
.
Finished in 0.026 seconds
1 test, 1 assertion, 0 failures, 0 skipped
但是不知何故,如果我的代码中有错误:
it('should not be dumb', function(done){
Promise.resolve(0).then(function(result){
console.log('dumb');
expect(2,toEqual(2));
done();
}, function (err){console.log(err); done()});
});
它只是坐在那里直到超时,没有任何有用的输出:
dumb
Pairing user stories - 7816 ms
should not be dumb - 7815 ms
Failures:
1) Pairing user stories should not be dumb Message:
timeout: timed out after 5000 msec waiting for spec to complete Stacktrace:
undefined
Finished in 46.884 seconds 1 test, 1 assertion, 1 failure, 0 skipped
我做错了什么?
最佳答案
假设:
promise.then(onResolve, onReject)
当 promise
被拒绝时,onReject
将被调用。但是,在您的代码中,拒绝发生在 onResolve
中(隐式地,通过引发错误),这不会影响 promise
(此时已经解决) ),因此不会调用 onReject
。
如果您想捕获 onResolve
中抛出的错误,您需要通过向您的 Promise 链添加另一个拒绝处理程序来处理它们:
Promise.resolve(0).then(function(result){
console.log('dumb');
expect(2,toEqual(2));
done();
}).then(null, function (err) {
console.log(err);
done()
});
如果 Promise
恰好是 bluebird
,您可以使用 .catch()
稍微缩短它:
Promise.resolve(0).then(function(result){
console.log('dumb');
expect(2,toEqual(2));
done();
}).catch(function (err) {
console.log(err);
done()
});
关于node.js - Jasmine Node 异步不理解代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32385496/