使用"=="进行空手道比赛的最佳方法和“包含"使用通用脚本 [英] Best way to do Karate match using "==" and "contains" using generic script

查看:59
本文介绍了使用"=="进行空手道比赛的最佳方法和“包含"使用通用脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题来自这里的上一个问题

让我们说我们实现的服务器v1和v2响应如下所示

Lets says our implemented server v1 and v2 response looks as follows

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

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

Similarly we define the client schema for v1 and v2 like as follows

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

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

From the above given data, all I want is to test following three cases in a single generic line and they must pass

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. 

中查看我的建议.之前的问题,也许是实现该问题的可能方法.

See my proposed suggestion in previous question for maybe possible ways to achieve this.

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

I have already tried the solution noted in previous question using karate.filterKeys(), however the third case will fail because it focuses on filtering the keys not the comparison itself so the below last line will not be able to test all three cases above.

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

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

For an accepted answer all three case must pass

推荐答案

看起来像您设计过度了您忘记了contains:P

Looks like you over-engineered so much you forgot about 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)

这篇关于使用"=="进行空手道比赛的最佳方法和“包含"使用通用脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆