首页 > 解决方案 > 空手道:是否可以比较两个服务响应并在比较中排除一些键

问题描述

我正在尝试使用空手道比较来自两个服务调用(用于迁移项目)的响应。对于以下情况,是否可以在一次比较整个响应时排除或忽略某些键:

1)响应值不同的元素很少(以粗体标记)。2)很少有元素存在于一个响应中但存在于另一个响应中(以粗体标记)。

服务 1 响应 -

{
  "userKey": "string",
  "enabled": true,
  "locked": true,
  "profile": {},
  "roles": [
    {
      "roleKey": 3,
      "name": "Role",
      **"links": []**
    }
  ],
  "links": [
    {
      "rel": "self",
      **"href": "https://starhub1.com"**
    },
    {
      "rel": "self",
      **"href": "https://singtel1.com"**
    }
  ]
}

服务 2 响应 -

{
  "userKey": "string",
  "enabled": true,
  "locked": true,
  "profile": {},
  "roles": [
    {
      "roleKey": 3,
      "name": "Role"
    }
  ],
  "links": [
    {
      "rel": "self",
      **"href": "https://starhub22.com"**
    },
    {
      "rel": "self",
      **"href": "https://singtel22.com"**
    }`enter code here`
  ]
}

标签: karate

解决方案


是的,空手道在这方面做得很好。只需在比较之前对一个有效负载进行转换。

有关详细信息,请参阅此答案:https ://stackoverflow.com/a/53120851/143475

对于您的特定示例,您可能需要做的就是将“一侧”更改为#string然后进行比较。

* def response1 = { foo: 'hello' }
* def response2 = { foo: 'world' }
* response2.foo = '#string'
* match response1 == response2

推荐阅读