【问题标题】:Chai/Mocha: My test will not throw an error despite recognizing an AssertionErrorChai/Mocha:尽管识别到 AssertionError,但我的测试不会抛出错误
【发布时间】:2019-10-18 05:09:40
【问题描述】:

我正在编写一些测试并且事情进展顺利,但是如果我尝试断言我知道是非常错误的东西,测试将通过,但我也会收到编译器抛出 AssertionError 的通知。但是,它实际上并没有通过测试。

我尝试返回 expect 短语,但我有点困惑为什么它不起作用,感觉应该很简单。

describe('api/users/changeAccountDetails', function() {
    beforeEach(()=>{
        return chai.request(app)
        .post('/api/users/signup')
        .send({
            firstName,
            lastName,
            username,
            password
        })
        .then(res => {
            console.log('a ok');
        })
        .catch(err => console.error(err));

    });

    afterEach(()=> {
        return User.deleteOne({})
    })


    describe('POST', ()=>{
        it('should update the firstName when given a string', ()=>{

        return chai.request(app)
        .post('/api/users/changeAccountDetails')
        .send({
            username,
            firstName: "Samus",
            lastName
        })
        .then(res => {
            console.log('b ok');
            //TODO: figure out why this isn't throwing an error if I make the number something else

            console.log(res.body);
            expect(res.body.code).to.equal(201);
            expect(res.body.user.firstName).to.equal('Michale');


        })
        .catch(err => console.error(err));  
        })
    });
})

响应如下所示:

b ok
{ code: 201,
  user:
   { firstName: 'Samus',
     lastName: 'User',
     _id: '5da947f7544bce4b6d71111f',
     username: 'exampleUser',
     password:
      '$2a$10$ABQzLOInfORJjsKd5Q3A9ejutCo22EVThHYLsEPPbqpVK717yJNGy',
     cats: [],
     __v: 0 } }
{ AssertionError: expected 'Samus' to equal 'Michale'
    at chai.request.post.send.then.res (/home/adrian/Development/gdc2API/test/test-users.js:186:52)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  message: 'expected \'Samus\' to equal \'Michale\'',
  showDiff: true,
  actual: 'Samus',
  expected: 'Michale' }
        ✓ should update the firstName when given a string

closing the server

【问题讨论】:

    标签: node.js mongodb mocha.js chai


    【解决方案1】:

    抛出的错误实际上被以下代码块捕获:

    .catch(err => console.error(err));
    

    由于该块,错误不会传播,因此测试成功。删除该块应该可以解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多