【发布时间】:2019-04-23 06:54:40
【问题描述】:
我在 Mocha 和 Supertest 中使用 async-await 语法。这是我有问题的测试:
it('Test POST/friends route: should add a friendship', async function () {
const lastFriendship = await models.Friendship.findAll({limit: 1,where: {},order: [ [ 'createdAt', 'DESC' ]]})
const lastFriendId = lastFriendship[0].id
await request(app)
.post('/friends')
.set('Authorization', token)
.send({ friendId: 998 })
.expect(200)
.expect(async (res) => {
console.log('res.data', res.body.data.id)
const newFriendId = res.body.data.id
expect(res.body.data.friendId).toEqual(998)
expect(res.body).toHaveProperty('error', null);
expect(newFriendId - lastFriendId).toBe(2)//This should fail the test.
})
})
现在写的最后一个断言应该无法通过测试,但是我收到了这个错误:
UnhandledPromiseRejectionWarning:错误: expect(received).toBe(expected) // Object.is 相等
我尝试将整个东西放在 try-catch 块中,但没有成功。我显然错过了这里的机制。
我的代码有什么问题?
编辑:从期望的回调函数中删除“异步”,解决它,虽然我不知道为什么在回调中抛出异常不会在这里被捕获。
【问题讨论】:
标签: node.js async-await mocha.js supertest