如何在API测试中检查属性的顺序

发布于 2025-02-13 21:58:15 字数 867 浏览 1 评论 0原文

这是重新订购的身体。我该如何断言以检查array [3]中的特定订单

{
                      "dimName": "Women's Clothing",
                      "dimOrder": 2,
                      "dimType": "wa",
                      "dimId": "category#womens-clothing",
                      "dimParent": "category"
                    },
                    {
                      "dimName": "Jewelry 1",
                      "dimOrder": 1,
                      "dimType": "wa",
                      "dimId": "category#jewelry",
                      "dimParent": "category"
                    },
                    {
                      "dimName": "Handbags",
                      "dimOrder": 3,
                      "dimType": "wa",
                      "dimId": "category#handbags",
                      "dimParent": "category"
                    }

This is the body for reorder. How can I do assertions to check the order for particular in array[3]

{
                      "dimName": "Women's Clothing",
                      "dimOrder": 2,
                      "dimType": "wa",
                      "dimId": "category#womens-clothing",
                      "dimParent": "category"
                    },
                    {
                      "dimName": "Jewelry 1",
                      "dimOrder": 1,
                      "dimType": "wa",
                      "dimId": "category#jewelry",
                      "dimParent": "category"
                    },
                    {
                      "dimName": "Handbags",
                      "dimOrder": 3,
                      "dimType": "wa",
                      "dimId": "category#handbags",
                      "dimParent": "category"
                    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2025-02-20 21:58:17

如果您在API测试中收到上述作为JSON响应,则几个快速示例:

检查ID的顺序,

cy.request(...)
  .then(response => {
    expect(response[0].dimId).to.eq('category#womens-clothing')
    expect(response[1].dimId).to.eq('category#jewelry')
    expect(response[2].dimId).to.eq('category#handbags')
  })

如下

cy.request(...)
  .then(response => {
    const dimOrder = response.map(item => item.dimOrder)
    expect(dimOrder).to.deep.eq([1,2,3])        // deep because is array matching
  })

If you received the above as a json response in an API test, a couple of quick examples:

Check the order of id's like this

cy.request(...)
  .then(response => {
    expect(response[0].dimId).to.eq('category#womens-clothing')
    expect(response[1].dimId).to.eq('category#jewelry')
    expect(response[2].dimId).to.eq('category#handbags')
  })

Check the dimOrder field is sequential like this

cy.request(...)
  .then(response => {
    const dimOrder = response.map(item => item.dimOrder)
    expect(dimOrder).to.deep.eq([1,2,3])        // deep because is array matching
  })
以为你会在 2025-02-20 21:58:17

为了更轻松地断言响应,尤其是嵌套属性,您可以使用 cy> cys-spok 。也更快地理解。

const spok = require('cy-spok')

cy.request(...)
  .its('response.body')
  .should(spok({
     propertieWithArray: [
       {
         // you can assert the properties equal exact values
         dimName: "Women's Clothing", 
         dimOrder: 2,
         dimType: "wa",
         dimId: "category#womens-clothing",                      
         dimParent: "category" 
       },
       {
         // you can assert properties meet specifications
         dimName: spok.string, // Jewelry 1
         dimOrder: spok.number, // 1
         dimType: spok.type('string'), // wa
         dimId: spok.startsWith('category'), // category#jewelry
         dimParent: spok.endsWith('category') // category
       },
       {
         // use combination
         dimName: spok.string,
         dimOrder: spok.gt(0),
         dimType: "wa",
         dimId: spok.test(/#/),
         dimParent: "category"
       }

For easier assertions on a response, especially nested properties, you can use cy-spok. It's also quicker to comprehend.

const spok = require('cy-spok')

cy.request(...)
  .its('response.body')
  .should(spok({
     propertieWithArray: [
       {
         // you can assert the properties equal exact values
         dimName: "Women's Clothing", 
         dimOrder: 2,
         dimType: "wa",
         dimId: "category#womens-clothing",                      
         dimParent: "category" 
       },
       {
         // you can assert properties meet specifications
         dimName: spok.string, // Jewelry 1
         dimOrder: spok.number, // 1
         dimType: spok.type('string'), // wa
         dimId: spok.startsWith('category'), // category#jewelry
         dimParent: spok.endsWith('category') // category
       },
       {
         // use combination
         dimName: spok.string,
         dimOrder: spok.gt(0),
         dimType: "wa",
         dimId: spok.test(/#/),
         dimParent: "category"
       }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文