Chai/Mocha:尽管识别出断言错误,但我的测试不会抛出错误



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

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

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

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

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

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

最新更新