首页 > 解决方案 > 空手道 - 比较多个 JSON 对象架构

问题描述

我正在尝试验证嵌套了多个 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”对象的模式以及“产品”的内部对象

标签: javatestingautomationkarate

解决方案


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

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

推荐阅读