柴/摩卡:尽管认识到AssertionError,我的测试也不会引发错误

时间:2019-10-18 05:09:40

标签: node.js mongodb mocha chai

我正在编写一些测试,并且一切顺利,但是,如果我尝试断言我所知道的完全错误的内容,则该测试将通过,但是我还将得到一个通知,指出编译器正在抛出AssertionError。但是,它实际上并没有通过测试。

我尝试返回期望的短语,但是我对为什么它不起作用感到困惑,感觉应该很简单。

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

1 个答案:

答案 0 :(得分:0)

抛出的错误实际上是由以下块捕获的:

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

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