【问题标题】:Nested JSON response Validation嵌套 JSON 响应验证
【发布时间】:2020-01-13 21:22:22
【问题描述】:
JSON Response

{
Feed[
      {"item_type":"cycle","id":123},
      {"item_type":"computer","name":"mac"},
      {"item_type":"Bag","weight":"2"}
  ]
}

As nested schema is changing based on item type

so can we compare schema based on item type?

example: if item_type is cycle then it should compare with id,
 if item_type is computer then it should compare name like this

我尝试过以下方法:

* def schema = {item_type:"#string",id:'##number',name: '##string',weight:'##string'}

在这个模式验证中,即使 item_type 是 cycle 并且 name 是 mac,测试用例也会通过,但实际上它应该会失败

【问题讨论】:

    标签: api karate


    【解决方案1】:

    首先让我建议尝试进行这些极端的“通用”验证可能是浪费时间。看到这个答案:https://stackoverflow.com/a/54126724/143475

    但这是一种可能的解决方案,可能还有其他方法。

    请阅读文档并研究其他答案以正确理解这一点。这使用了第二个功能文件。

    第一个功能文件:

    * def response = 
    """
    [
      { "item_type": "cycle", "id": 123 },
      { "item_type": "computer", "name": "mac" },
      { "item_type": "bag", "weight": "2" }
    ]
    """
    * print response
    * def data = 
    """
    {
      cycle: { id: '#number' },
      computer: { name: '#string' },
      bag: { weight: '#string' } 
    }
    """
    * def isValid = function(x){ karate.call('called.feature', x) }
    * karate.forEach(response, isValid)
    

    第二个特征文件(称为called.feature):

    @ignore
    Feature:
    
    Scenario:
    * def expected = data[__arg.item_type]
    * match __arg contains expected
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 2016-08-21
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-31
      相关资源
      最近更新 更多