1

我正在使用 Chai http 和 promise。下面的测试应该失败,但它没有调用 then 函数就通过了。如果我添加 done 参数以等待异步函数完成,它会失败(正确)。难道我做错了什么?

it('Returns the correct amount of events', function() {
    chai.request(app)
        .get('/api/events/count')
        .then(function(res) {
            throw new Error('why no throw?');
            expect(res).to.have.status(200);
            expect(res).to.be.json;
        })
        .catch(function(err) {
            throw err;
        });
});
4

1 回答 1

1

当您忘记返回承诺时,您的测试是常青的。因此,您只需要返回 promise 即可使其工作:

it('Returns the correct amount of events', function() {
  return chai.request(app)
    .get('/api/events/count')
    .then(function(res) {
        throw new Error('why no throw?');
        expect(res).to.have.status(200);
        expect(res).to.be.json;
    })
    .catch(function(err) {
        return Promise.reject(err);
    });
});
于 2018-07-08T21:41:14.107 回答