Home > Software engineering >  what are the assertions that are used for check the order in below body in api tesing using cypress
what are the assertions that are used for check the order in below body in api tesing using cypress

Time:07-09

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"
                    }

CodePudding user response:

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
  })

CodePudding user response:

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"
       }
  • Related