即使结果不如预期,摩卡茶也通过了测试

问题描述 投票:0回答:1

我使用 mocha、chai、express 来测试 REST API 响应 响应状态是 201,我预计测试会失败

it('janus post', () => {
  request('https://***')
    .post('/***')
    .attach('file', 'T:/***/Test1.docx')
    .end(function(err, res) {
      if (err) throw err;
      expect(res.status).to.be.equal(200)
      console.log(res.body);           
  });
});

测试通过,结果是:

     First test

        ✔ janus post


      1 passing (399ms)

    C:\projects\testacademyapi\node_modules\mocha\lib\runner.js:950
        throw err;
        ^
    AssertionError: expected 201 to equal 200
        at Test.<anonymous> (C:\projects\apitests\tests\webApi.spec.js:28:38)
        at Test.assert (C:\projects\apitests\node_modules\supertest\lib\test.js:172:8)
        at localAssert (C:\projects\apitests\node_modules\supertest\lib\test.js:120:14)
        at C:\projects\apitests\node_modules\supertest\lib\test.js:125:7
        at Request.callback (C:\projects\apitests\node_modules\superagent\lib\node\index.js:857:12)
        at C:\projects\apitests\node_modules\superagent\lib\node\index.js:1070:18
        at IncomingMessage.<anonymous>    (C:\projects\apitests\node_modules\superagent\lib\node\parsers\json.js:21:7)
        at IncomingMessage.emit (node:events:526:35)
        at endReadableNT (node:internal/streams/readable:1359:12)
        at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
      showDiff: true,
      actual: 201,
      expected: 200,
      operator: 'strictEqual'
    }

我预计测试会失败。

javascript node.js express mocha.js chai
1个回答
0
投票

我修改了代码,添加了done() 现在可以了。

现在代码如下所示:

it('janus post', (done) => {
  request('https://***')
    .post('/***')
    .attach('file', 'T:/***/Test1.docx')
    .end(function(err, res) {
      if (err) throw err;
      expect(res.status).to.be.equal(200)
      done();
      console.log(res.body);           
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.