首页 文章

错误:在使用Chai和Mocha发送 Headers 后无法设置 Headers

提问于
浏览
0

我正在使用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:未处理的promise promise(拒绝ID:1):错误:发送后无法设置标头 .

1 回答

  • 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' });
    }
    

相关问题