空手道-比较多个JSON对象架构

时间:2019-08-15 08:45:11

标签: java testing automation karate

我正在尝试验证嵌套了多个JSON对象的JSON。 例子

  Scenario: temp1
* def response1 =
"""
{
"productGroups": [
    {
        "dateLabel": "28 Aug, Wed",
        "products": [
            {
                "id": 1439,
                "product": "product 1"
            },
            {
                "id": 1401,
                "product": "product 2"
            }
        ]
      }
    ]
  }
"""
* print response1.productGroups
Then match response1.productGroups[*] contains
"""
{
  'dateLabel': #string,
  'products': [
    {
      'id': #number,
      'product': #string
    }
  ]
}
"""

的形式获取响应
  

原因:实际值不包含预期值

如果我将验证更改为

Then match response1.productGroups[0] contains

的形式获取响应
  

原因:实际数组和预期数组的大小不同-2:1

我想做的是验证“ productGroups”对象的架构以及“ products”的内部对象

1 个答案:

答案 0 :(得分:1)

请花一些时间阅读文档,这是值得的:https://github.com/intuit/karate#schema-validation

* def product = { 'id': #number, 'product': #string }
Then match response1.productGroups[*] contains
"""
{
  'dateLabel': #string,
  'products': '#[] product'
}
"""