最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

错误:超出2000毫秒的超时。对于异步测试和挂钩,请确保“done()”

IT培训 admin 3浏览 0评论

错误:超出2000毫秒的超时。对于异步测试和挂钩,请确保“done()”

码:

var processFooBar = function (message, callback) {

  doFooAndBar(message, callback);

};
module.exports.processFooBar = processFooBar;


var doFooAndBar = function (data, callback) {
    async.parallel(
        [
            function (callback) {
                foo(data, function (err, response) {
                    callback(err, response);
                });
            },
            function (callback) {
                bar(data, function (err, response){
                    callback(err, response);
                });
            }
        ],
        function (err, results) {
            callback(err, results);
        }
    );
};
module.exports.doFooBar = doFooBar;

单元测试

describe('Process data', function () {
    var fooStub;

    beforeEach(function (done) {
        done();
    });

    afterEach(function (done) {
        fooStub.restore();
        done();
    });

    it('can process data', function (done) {
        fooStub = sinon.stub(fileName, 'foo').yields(null, null);
        barNockCall();
        app.processFooBar(message,
            function (err, response) {
                nock.isDone().should.be.true;
                nock.cleanAll();
                done();
            }
        }
    });

我收到以下错误:

can process data:
  Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. 
(/Path/To/Test.js)

如果我在async.parallel中删除foo(),那么我没有得到错误。另外,我猜第一个是fooStubis的sinon.stub没有被调用。

回答如下:

您需要增加测试框架的超时。它的默认超时可能为2000毫秒,如果请求超过2秒,则会抛出错误。

beforeEach(function (done) {
 this.timeout(10000)
 done();
});

覆盖默认超时可能适用于您的情况。

错误:超出2000毫秒的超时。对于异步测试和挂钩,请确保“done()”

码:

var processFooBar = function (message, callback) {

  doFooAndBar(message, callback);

};
module.exports.processFooBar = processFooBar;


var doFooAndBar = function (data, callback) {
    async.parallel(
        [
            function (callback) {
                foo(data, function (err, response) {
                    callback(err, response);
                });
            },
            function (callback) {
                bar(data, function (err, response){
                    callback(err, response);
                });
            }
        ],
        function (err, results) {
            callback(err, results);
        }
    );
};
module.exports.doFooBar = doFooBar;

单元测试

describe('Process data', function () {
    var fooStub;

    beforeEach(function (done) {
        done();
    });

    afterEach(function (done) {
        fooStub.restore();
        done();
    });

    it('can process data', function (done) {
        fooStub = sinon.stub(fileName, 'foo').yields(null, null);
        barNockCall();
        app.processFooBar(message,
            function (err, response) {
                nock.isDone().should.be.true;
                nock.cleanAll();
                done();
            }
        }
    });

我收到以下错误:

can process data:
  Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. 
(/Path/To/Test.js)

如果我在async.parallel中删除foo(),那么我没有得到错误。另外,我猜第一个是fooStubis的sinon.stub没有被调用。

回答如下:

您需要增加测试框架的超时。它的默认超时可能为2000毫秒,如果请求超过2秒,则会抛出错误。

beforeEach(function (done) {
 this.timeout(10000)
 done();
});

覆盖默认超时可能适用于您的情况。

发布评论

评论列表 (0)

  1. 暂无评论