【问题标题】:Mocha tests are passing but assertion or expect is failingMocha 测试通过但断言或期望失败
【发布时间】:2021-12-31 19:44:49
【问题描述】:

我正在使用 expectassert 都显示为错误,这是我想要的,我希望测试失败。但是由于某种原因,当我使用任何一个时,测试都会通过。我尝试了expect,然后尝试了assert。不知道为什么会这样。我从请求中返回的数据是正确的,但只是断言和/或期望不起作用。

const assert = require('assert');
const expect = require('chai').expect;
const request = require('supertest');
const server = require('../server');

describe('Unit testing the /api/auth/signup route', function () {
  it('Should return OK status', async () => {
    try {
      let res = await request(server).post('/api/auth/signup').send({
        email: 'tom@email.com',
        password: 'tompassword',
      });

      // assert.equal(res.body.data.user.email, 'josh@email.com');
      expect(res.body.data.user.email).to.equal('josh@email.com');
    } catch (err) {
      console.log(err);
    }
  });
});

【问题讨论】:

    标签: express mocha.js assert


    【解决方案1】:

    assertexpect 都会抛出错误(Node 中的 AssertionError)来报告未满足的期望。 Mocha 会捕获测试函数抛出的所有错误并报告测试失败。

    您不应将这些语句包装在 try/catch 块中。这样做会抑制异常,Mocha 会认为测试通过了。

    【讨论】:

    • 谢谢。没有意识到在这种情况下我不必包含 try/catch 块。
    猜你喜欢
    • 2019-04-11
    • 1970-01-01
    • 2021-12-18
    • 2017-07-18
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 2020-08-06
    • 2017-03-04
    相关资源
    最近更新 更多