我正在尝试测试是否可以在我的api中发布一些东西。但是,它正在返回未定义的值。我用Hoppscotch测试过,对象在那里,只是测试没有通过
测试结果

expect(received).toEqual(expected) // deep equality
Expected: {"height": 5.1, "id": 5, "lightsaber": null, "name": "chewie"}
Received: undefined
49 | // console.log(res.body)
50 | // })
> 51 | expect(api.body).toEqual(testData)
| ^
52 |
53 | })describe('api server', () => {
let api;
test ('responds to post /starwars with status 201', () =>{
const testData = {
id: 5,
name: 'chewie',
height: 5.1,
lightsaber: null
}
request(api)
.post('/starwars')
.send(testData)
.set('Accept', 'application/json')
.expect(201)
expect(api.body).toEqual(testData)
})
})发布于 2022-08-21 12:32:04
您必须将api请求保存到变量中,而不是使用它来比较是否等于以下值:
test ('responds to post /starwars with status 201', async () =>{
const testData = {
id: 5,
name: 'chewie',
height: 5.1,
lightsaber: null
}
const response = await request(api)
.post('/starwars')
.send(testData)
.set('Accept', 'application/json')
.expect(201)
expect(response.body).toEqual(testData)
})https://stackoverflow.com/questions/73434343
复制相似问题