node.js - Chai/Mocha:尽管识别到 AssertionError,但我的测试不会抛出错误
问题描述
我正在编写一些测试,一切进展顺利,但如果我尝试断言我知道是非常错误的东西,测试将通过,但我也会收到编译器抛出 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
解决方案
抛出的错误实际上被以下块捕获:
.catch(err => console.error(err));
由于该块,错误不会传播,因此测试成功。删除该块应该可以解决您的问题。
推荐阅读
- r - 如何使 R 中的嵌套 for 循环将输出写入数据帧更有效?
- azure - 为什么 Azure 在虚拟机上提供多个删除锁?
- css - Material UI Theme 中的前景色和背景色
- payment-gateway - 如何在 Square Payment Gateway 中设置每 2 个月或 3 个月订阅一次
- flutter - 颤动的文本小部件内的几个单词之间的字母间距太大。如何删除不需要的空间?
- php - 适配mysqli代码的mysql代码留下页面空白
- reactjs - 在 useEffect 中使用额外依赖的后果是什么?
- xquery - MarkLogic:如何获取数据库中所有文档的计数,并具有 rest-reader、rest-writer 权限?
- c++ - C++ -> 哪个更快?诠释一个(5);或int a = 5;
- c++ - 如何创建模板嵌套模板参数包?