使用常规脚本使用“ ==”和“包含”进行空手道匹配的最佳方法

时间:2019-07-26 17:29:37

标签: dsl karate

此问题是由previous question here

引起的

让我们说我们实现的服务器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. 

请参阅我在previous question中提出的建议,以获得实现此目标的可能方法。

我已经使用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

要获得可接受的答案,所有三个案例都必须通过

1 个答案:

答案 0 :(得分:1)

看起来像您over-engineered so much,而您忘记了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)