使用“==”和 "contains" 使用通用脚本进行空手道比赛的最佳方式

Best way to do Karate match using "==" and "contains" using generic script

这个问题是由

假设我们实施的服务器 v1 和 v2 响应如下所示

* def v1Response = { id: "1", name: "awesome" }
* def v2Response = { id: "2", name: "awesome", value: "karate" }

类似地,我们为 v1 和 v2 定义客户端架构,如下所示

* def v1Schema = { id: "#string", name: "#string }
* def v2Schema = { id: "#string", name: "#string, value: "#string" }

根据上面给出的数据,我只想在一个通用行中测试以下三种情况,它们必须通过

1. * match v1Response == v1Schema
2. * match v2Response == v2Schema 
3. * match v2Response contains v1Schema

如下使用单个通用行

* match response ==/contains schema <--- should be able to test all above three cases above and they must pass. 

请参阅我在 中提出的建议,了解可能的实现方法。

我已经使用 karate.filterKeys() 尝试了上一个问题中提到的解决方案,但是第三种情况将失败,因为它侧重于过滤键而不是比较本身,因此下面的最后一行将无法测试以上所有三种情况。

* def response = { id: "2", name: "awesome", value: "karate" } 
* def schema = { id: "#string", name: "#string" } 
* match response == karate.filterKeys(schema, response) <--- This will fail

对于一个被接受的答案,所有三个案例都必须通过

看起来你 你忘记了 contains :P

* def schemas =
"""
{
  v1: { id: "#string", name: "#string" },
  v2: { id: "#string", name: "#string", value: "#string" }
}
"""

* def env = 'v1'
* def response = { id: "1", name: "awesome" }
* match response contains karate.filterKeys(schemas[env], response)

* def response = { id: "2", name: "awesome", value: "karate" }
* match response contains karate.filterKeys(schemas[env], response)

* def env = 'v2'
* def response = { id: "1", name: "awesome" }
* match response contains karate.filterKeys(schemas[env], response)

* def response = { id: "2", name: "awesome", value: "karate" }
* match response contains karate.filterKeys(schemas[env], response)