Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asynchronous 测试Mocha/supertest/expect平均堆栈HTTP请求4秒延迟完成_Asynchronous_Testing_Mocha.js_Supertest - Fatal编程技术网

Asynchronous 测试Mocha/supertest/expect平均堆栈HTTP请求4秒延迟完成

Asynchronous 测试Mocha/supertest/expect平均堆栈HTTP请求4秒延迟完成,asynchronous,testing,mocha.js,supertest,Asynchronous,Testing,Mocha.js,Supertest,我正在使用Mocha/supertest/expect库在MEAN Stack中测试HTTP请求,需要4秒钟才能返回: it('should return product data', (done) => { request(app) .get('/P/Product') .expect(200) .expect((res) => { expect(res.body[0]._id).toEqual('123456789') })

我正在使用Mocha/supertest/expect库在MEAN Stack中测试HTTP请求,需要4秒钟才能返回:

it('should return product data', (done) => {
    request(app)
    .get('/P/Product')
    .expect(200)
    .expect((res) => {
        expect(res.body[0]._id).toEqual('123456789')
    })
    .end(done);
});
函数应该在HTTP请求完成后的最后执行“done”回调。但我得到了一个错误:

1) should return product data:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a
Promise, ensure it resolves. 

我认为它适用于不受欢迎的expect调用。我如何处理其他expect调用中的嵌套expect调用,例如abouve?

可能请求的响应时间太长,导致Mocha默认的2秒测试超时。可能会尝试从CLI对url进行卷曲,以查看返回或启动测试的时间

describe('testing', function() {
    // This applies the timeout of 5 seconds to each test.
    this.timeout(5000);

    it('should return product data', function() {
       // This applies a timeout of 5 seconds for this test only
       this.timeout(5000);
       request(app)
           .get('/P/Product')
           .expect(200)
           .expect((res) => {
                expect(res.body[0]._id).toEqual('123456789')
           })
           .end(done);
       });
});
如果
expect
调用链导致超时问题,另一种方法是使用promise方法

it('should return product data', () => {
   request(app)
   .get('/P/Product')
   .then((res) => {
       expect(res.status).to.equal(200);
       expect(res.body[0]._id).to.equal('123456789');
   })
});

我得到了解决方案,问题是使用了arrow函数:
(done)=>{…}
,而不是普通的回调,它的工作原理如下:

it('should async square a number', function(done) {  
    this.timeout(2005);
    utils.asyncSquare(5, (res) => {
      expect(res).toBe(25).toBeA('number');
      done();
    });
  });
或者,如果在包测试脚本中全局设置超时,它也可以工作:

"test": "mocha --timeout 3000 **/*.test.js",

嗯,你能给我举个例子,如何设置摩卡咖啡的测试时间阈值吗?beforeach(函数(完成){this.timeout(3000);setTimeout(完成,3000);这不起作用……我编辑了我的答案,以提供如何在套件级别或单个测试级别使用设置超时的示例嘿,感谢这个示例,我在代码中尝试了相同的设置,但我得到:TypeError:this.timeout不是一个函数……我在谷歌上搜索了一下,似乎许多其他人都有相同的问题,有解决方法吗为此?如果您使用timout选项运行摩卡,它实际上可以在全球范围内运行:“摩卡--超时15000”但不是在测试单元级别…奇怪的是,这在我自己的测试中对我有效,预计超过2秒。根据mocha文档,这也是超时的方法。您可能在
descripe
上使用箭头函数,这就是为什么
没有定义超时。使用
descripe('title',function(){…}
而不是
描述('title',()=>{…}
是@AndrewNolan回答了您问题的关键(使用
.timeout
)因此,我建议您删除您的答案,并接受他的答案。您使用箭头函数的事实与您遇到的原始问题无关。@AndrewNolan的解决方案不像他所展示的那样有效,您需要使用普通回调函数(完成)在it功能内部,但感谢它的缺点…没问题,我很乐意为那些蔑视他人所做工作的人再次这样做,因为他不知道如何使用箭头功能。嗯,事实上你是对的,我只是再次测试了它,它起了作用。我把我的右括号从顶层放错了,这就是问题所在…对不起,没有理由粗鲁,伙计;)你没有抓住重点。@AndrewNolan告诉过你以前不知道的
超时问题。这就是答案。如何让
超时
工作与问题无关。如果你认为他的答案可以改进,那么编辑它,但仍然接受它。没有人会因为帮你解决问题而得到报酬女士,你至少可以通过适当地授予声誉来表示你对这些努力的赞赏。