空手道如何在嵌套级别比较json对象

时间:2019-01-23 14:26:18

标签: karate

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

actualResponse:

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

expectedReponse:

 {
    "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

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

1 个答案:

答案 0 :(得分:2)

请阅读文档: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