空手道中的未命名JSON数组字段验证

时间:2019-03-22 20:56:58

标签: karate

我在响应中有一个这样的未命名JSON数组,并想检查它是否包含"confirmationNumber": "pqrs"。我可以在空手道中检查一下吗?

[
    {
        "id": 145,
        "confirmationNumber": "abcd"
    },{
        "id": 723
        "confirmationNumber": "pqrs"
    }
    ,{
        "id": 7342
        "confirmationNumber": "sfeq"
    }
]

1 个答案:

答案 0 :(得分:2)

karate.filter()适用于以下情况:

* def response = 
"""
[
   {
      "id":145,
      "confirmationNumber":"abcd"
   },
   {
      "id":723,
      "confirmationNumber":"pqrs"
   },
   {
      "id":7342,
      "confirmationNumber":"sfeq"
   }
]
"""
* def fun = function(x){ return x.confirmationNumber == 'pqrs' }
* def found = karate.filter(response, fun)
* match found == '#[1]'

另请参阅JsonPath的示例:https://github.com/intuit/karate#jsonpath-filters

编辑:抱歉,有一种简单得多的方法,请read the docs

* match response contains { id: '#number', confirmationNumber: 'pqrs' }
* def item = { confirmationNumber: 'pqrs' }
* match response contains '#(^item)'