简体   繁体   English

错误:超时超过2000毫秒。 承诺的单元测试

[英]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.

相关问题 错误:超出2000毫秒的超时。 保证在测试运行之前不返回 - Error: Timeout of 2000ms exceeded. promise not returning before test runs 错误:超过 2000 毫秒的超时。 对于异步测试和钩子,在 nodejs 中使用 Mocha 进行测试时确保“done()” - Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure “done()” when using Mocha for test in nodejs Mocha&Chai-超过2000毫秒的超时。 确保在此测试中调用done()回调。“ - Mocha & Chai- “timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.” 噩梦:错误:超时超过2000毫秒。 对于异步测试和挂钩 - Nightmare: Error: Timeout of 2000ms exceeded. For async tests and hooks 摩卡:超过 2000 毫秒的错误超时 - Mocha: Error Timeout of 2000ms exceeded 摩卡:错误:超过 2000 毫秒超时。 对于异步测试和钩子,确保调用“done()”; 如果返回 Promise,请确保它已解决 - Mocha: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves Selenium总是得到'错误:超过2000毫秒的超时' - Always getting 'Error: timeout of 2000ms exceeded' with Selenium 在Mocha中收到“超过2000ms超时”错误 - Getting 'Timeout of 2000ms exceeded' error in mocha 2000ms的超时超过了摩卡 - timeout of 2000ms exceeded mocha 摩卡测试超时2000ms - Mocha test.Timeout 2000ms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM