我正在使用Express测试Mocha和Chai,但在测试两个错误处理路径时我一直遇到此错误。测试仍然通过,但我仍然收到错误消息。不确定如何在测试中处理它
describe.only('allYears services', () => {
beforeEach(async () => {
await db.sequelize.sync({force: true, logging: false});
});
it('should response with 400 when missing gaugeId or classId', async () => {
await chai
.request(app)
.post('/api/allyears/getBoxPlotAttributes')
.send({metric: 'average'})
.catch(err => {
assert.equal(err.response.status, 400);
});
});
it('should response with 400 when missing metric', async () => {
await chai
.request(app)
.post('/api/allyears/getBoxPlotAttributes')
.send({gaugeId: 123456})
.catch(err => {
assert.equal(err.response.status, 400);
});
});
});
我收到以下错误消息:
(node:95031)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):错误:发送后无法设置标头。
答案 0 :(得分:0)
确保返回例如。 res.status(200).send({ something });
这会在使用Mocha + Chai进行测试时给您错误:
if (isInvalid(req)) {
res.status(400).send({ success: false, message: 'invalid request' });
}
这正常工作:
if (isInvalid(req)) {
return res.status(400).send({ success: false, message: 'invalid request' });
}