首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我测试POST api时没有收到任何消息

当我测试POST api时没有收到任何消息
EN

Stack Overflow用户
提问于 2022-08-21 12:19:49
回答 1查看 39关注 0票数 1

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

测试结果

代码语言:javascript
复制
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 |     })
代码语言:javascript
复制
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)

    })

})
EN

回答 1

Stack Overflow用户

发布于 2022-08-21 12:32:04

您必须将api请求保存到变量中,而不是使用它来比较是否等于以下值:

代码语言:javascript
复制
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)

    })
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73434343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档