【问题标题】:Un-named JSON array field validation in Karate空手道中未命名的 JSON 数组字段验证
【发布时间】:2019-03-22 20:56:58
【问题描述】:

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

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

【问题讨论】:

    标签: karate


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多