我用chai-http做了一个简单的测试,在其中我尝试使用async.each测试多个URL,但是当请求花费2秒钟以上时,我得到了错误。

it("it should GET the required images", (done) => {
    async.each(get_data, function(item, cb){
      chai
        .request(item.server_url.S)
        .get('/'+ item.endpoint.S + '?' + item.incoming.S)
        .end(function(err, res) {
          if(err) console.error(err);
          expect(err).to.be.null;
          expect(res).to.have.status(200);
          cb();
        });
    }, function(err){
      if(err) console.log(err);
      done();
    });
  });


我以为是正确的称呼“完成”,但是我不断收到错误消息,我在做什么错?即使没有异步,也只有一个简单的chai请求,只有一个请求,错误仍在显示...因此,可以肯定的是,这不是一个异步问题,但是我使用chaiHttp不好。

我也尝试用“ then / catch”代替“ end”,但是结果是一样的。

我有一个类似的问题,在相同的测试脚本中,但在数据库中,如果查询花费的时间超过2秒,则会中断...相同的错误,也使用“完成”:

before((done) => {
  // runs before all tests in this block
  const params = {
    TableName: "mytable"
  };

  mydb.scan(params, (err, records) => {
    if(err) console.log(err);
    for(let i = 0; i < records.Items.length; i++){
      //...some ifs, nothing async
    }
    done();
  });
});

最佳答案

如果您的测试时间超过2000毫秒,请考虑延长超时时间,因为这可能会解决您的问题

it("it should GET the required images", (done) => {
   this.timeout(5000);
   //...

08-07 11:45