Проверка JSON на основе ответа от arrayElement

Я хотел проверить из формата ответа, если status=AVAILABLE, затем arrayElement должен возвращаться с roomTypeId, иначе roomTypeId не должен возвращаться для другого состояния.

[
    {
        "status": "SOLDOUT",
        "propertyId": "dc00e77f",
        "Fee": 0,
        "isComp": false
    },
    {
        "roomTypeId": "c5730b9e",
        "status": "AVAILABLE",
        "propertyId": "dc00e77f",
        "price": {
            "baseAveragePrice": 104.71,
            "discountedAveragePrice": 86.33
        },
        "Fee": 37,
        "isComp": false
    },
    
]

[{"status": "SOLDOUT", "propertyId": "773000cc-468a-4d86-a38f-7ae78ecfa6aa", "resortFee": 0, "isComp": false}, {"roomTypeId": "c5730b9e-78d1-4c1c -a429-06ae279e6d4d "," status ":" AVAILABLE "," propertyId ":" dc00e77f-d6bb-4dd7-a8ea-dc33ee9675ad "," price ": {" baseAveragePrice": 104.71," DiscountsAveragePrice"ResortFour" ResortFour ": 86.33} ": 37," isComp ": false},

]

Я пытался проверить это снизу;

pm.test("Verify if Status is SOLDOUT, roomTypeId and price information is not returned ", () => {
    var jsonData = pm.response.json();
    jsonData.forEach(function(arrayElement) {
      if (arrayElement.status == "SOLDOUT") 
              { 
                 pm.expect(arrayElement).to.not.include("roomTypeId");
              }
              else if (arrayElement.status == "AVAILABLE") 
              { 
                 pm.expect(arrayElement).to.include("roomTypeId");
              }
          });
});

1 ответ

Вы должны проверить, существует ли свойство или нет.

С have.property Синтаксис вы можете сделать это.

Вы можете прочитать справочные документы по API Postman, а также Postman использует внутреннюю ветвь chai, поэтому документы ChaiJS также должны помочь вам.

Обновленный скрипт:

pm.test("Verify if Status is SOLDOUT, roomTypeId and price information is not returned ", () => {
    var jsonData = pm.response.json();
    jsonData.forEach(function(arrayElement) {
        if (arrayElement.status === "SOLDOUT") {
            pm.expect(arrayElement).to.not.have.property("roomTypeId");
        } else if (arrayElement.status === "AVAILABLE") {
            pm.expect(arrayElement).to.have.property("roomTypeId");
        }
    });
});
Другие вопросы по тегам