我正在使用Mocha和Chai进行单元测试。我的测试用例如下所示。
describe('1. Users APIs', () => {
it('1.1 Get Users', (done) => {
controller.getCaseDetails(req, res, () => { }).then(data => {
except(data).to.be.have.property(statusCode, 400);
done();
});
});
});
如果测试用例失败,那么我将得到错误提示。
1) 1. Users APIs.
1.1 Get Users:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/techjini/Techjini/node-projects/CollectionsApp-NodeJS/src/cases/test/getCases.test.js)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7)
为了解决这个问题,我添加了.catch
,然后在catch中阻止了done()
。然后测试成功了。如何解决此问题。
根据我的追踪,如果我添加catch块,则将处理该错误,但是如果我没记错的话,摩卡咖啡将不会出现任何断言错误。
except
失败时我想打印正确的错误消息。
任何帮助将不胜感激。
describe('Live Agent', function () {
//set timeout for this test suit
this.timeout(25000);
describe('createRequest', function () {
it(" Live agent request is created", function (done) {
supertestAgent
.post(apiPath + "/createRequest")
.send({
wildcard: 1,
"dialogId": "5e427fc25c790ffa5c727a",
"platform": "fb",
"endUser": { id: "fsfg" }
})
.end(function (err, result) {
expect(err).equals(null);
expect(result.status).equals(201)
done();
});
})
});