[英]Error: timeout of 2000ms exceeded. Unit test with promises
My unit tests make http requests to NET API and use kriskowal q library. 我的单元测试向NET API发出http请求,并使用kriskowal q库。 When I do
assert
call in then
callback and assertion fails I see Error: timeout of 2000ms exceeded
instead of AssertionError
. 当我
assert
呼叫then
回调断言失败,我看到Error: timeout of 2000ms exceeded
而不是AssertionError
。 I wrote example to illustrate this situation: 我写了一些例子来说明这种情况:
var assert = require('assert')
, Q = require('q');
it('promise', function(cb){
var deferred = Q.defer();
deferred.promise.then(function(){
assert(false);
cb();
});
deferred.resolve();
});
I can't understand this behaviour. 我无法理解这种行为。 Modelling asynchronous behaviour with
setTimeout/setImmediate
shows normal AssertionError
. 使用
setTimeout/setImmediate
异步行为进行建模显示了正常的AssertionError
。
Q does not provide unhandled rejection tracking, you need to explicitly .done
promises to signal a chain has ended. Q不提供未处理的拒绝跟踪,您需要显式
.done
承诺以信号链结束。 You get a suppressed error in your test: 您在测试中得到抑制的错误:
it('promise', function(cb){
var deferred = Q.defer();
deferred.promise.then(function(){
assert(false);
cb();
}).done(); // NOTE THE DONE
deferred.resolve();
});
Mocha however, provides better promise syntax, you can simply return a promise and have the rejection turn to a test failure: 但是,Mocha提供了更好的Promise语法,您可以简单地返回一个Promise并将拒绝变为测试失败:
it('promise', function(cb){
return new Q.Promise(function(resolve){ resolve(); }). // use the new syntax
then(function(){
assert(false);
cb();
});// no done needed because of the `return`
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.