【问题标题】:JSON Validate check based on response from arrayElement基于来自 arrayElement 的响应的 JSON 验证检查
【发布时间】:2019-05-10 04:04:21
【问题描述】:

我想检查响应格式,如果 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
    },
    
]

[ { “状态”:“售罄”, "propertyId": "773000cc-468a-4d86-a38f-7ae78ecfa6aa", “度假村费”:0, “isComp”:假 }, { "roomTypeId": "c5730b9e-78d1-4c1c-a429-06ae279e6d4d", “状态”:“可用”, "propertyId": "dc00e77f-d6bb-4dd7-a8ea-dc33ee9675ad", “价格”: { “baseAveragePrice”:104.71, “折扣平均价格”:86.33 }, “度假村费”:37, “isComp”:假 },

]

我试图从下面检查这个;

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

【问题讨论】:

    标签: postman postman-testcase


    【解决方案1】:

    您需要检查该属性是否存在。

    使用have.property 语法,您可以做到这一点。

    你可以阅读Postman API reference docsPostman uses a fork of chai internally,所以ChaiJS docs should also help you.

    更新的脚本:

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

    【讨论】:

      猜你喜欢
      • 2018-09-18
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多