使用空手道来匹配部分响应

时间:2018-10-01 15:07:49

标签: karate

我的响应可能包含以下递归对象:

"categoriesArray": [
  {
    "id": "category1",
    "href": "/categories/categoryId/category1",
    "name": "category name 1",

    "categoriesArray": [
      {
        "id": "category2",
        "href": "/categories/categoryId/category2",
        "name": "category name 2",
        }
    ]
    }
]

我想按以下顺序匹配所有ID:

  • def category1 = {id:'category1'}
  • def category2 = {id:'category2'} 并匹配response [0] .categoriesArray包含category1 并匹配response [0] .categoriesArray [0] .categoriesArray包含category2

那么RestAssured中有诸如appendRoot(“ response [0] .categoriesArray [0] .categoriesArray”)之类的快捷方式,可以在不重复路径的情况下更深入吗?

1 个答案:

答案 0 :(得分:1)

是的,使用JsonPath,您可以选择非常明智的选择。我认为这对您有用:

* def response =
"""
{
  "categoriesArray": [
    {
      "id": "category1",
      "href": "/categories/categoryId/category1",
      "name": "category name 1",
      "categoriesArray": [
        {
          "id": "category2",
          "href": "/categories/categoryId/category2",
          "name": "category name 2"
        }
      ]
    }
  ]
}
"""
* match response..id contains ['category1', 'category2']
* def cat1 = response.categoriesArray[0]
* match cat1..id contains 'category2'
* match response.categoriesArray[?(@.id=='category1')].categoriesArray..id contains 'category2'

编辑:添加了更多选项以回应评论