【问题标题】:How to do comparison of json object on nesting level in karate如何在空手道的嵌套级别上比较 json 对象
【发布时间】:2019-01-24 00:34:07
【问题描述】:

如何将嵌套的 json 响应与另一个响应进行比较

实际响应:

  {
    "status": "SUCCESS",
    "accountOrder": {
               "id": "11450027922158000",
               "accountType": "SAVINGS",
               "servicePrice": 400
               }
     }

预期响应:

 {
    "status": "SUCCESS",
    "accountOrder": {
               "accountType": "SAVINGS"
     }
     }

我尝试了以下方法进行比较:

   * match actualResponse contains expectedResponse // failed with reason: actual value has 2 more key(s) than expected

我们的要求是我们不想像这样硬编码比较

 * match actualResponse.accountType contains expectedResponse.accountType

我们可能需要在预期中添加更多属性并且不想频繁修改脚本

【问题讨论】:

标签: karate


【解决方案1】:

请阅读文档:https://github.com/intuit/karate#contains-short-cuts

* def accountOrder = { "accountType": "SAVINGS" }
* def expected = { status: SUCCESS, accountOrder: '#(^accountOrder)' }
* def response =
"""
{
  "status": "SUCCESS",
  "accountOrder": {
    "id": "11450027922158000",
    "accountType": "SAVINGS",
    "servicePrice": 400
  }
}
"""
* match response == expected

【讨论】:

    猜你喜欢
    • 2020-11-19
    • 1970-01-01
    • 2019-12-21
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2012-02-03
    相关资源
    最近更新 更多