4

我用 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();
  });
});
4

2 回答 2

3

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

it("it should GET the required images", (done) => {
   this.timeout(5000);
   //...
于 2017-01-26T03:11:24.730 回答
0

Give times in this.timeout() method as much as you need to finish the test

it('it should solve the timeout issue of a test in test level', function(done) {
    this.timeout(10000);
    done();
});
于 2019-09-14T05:34:01.297 回答