在空手道中使用条件逻辑无法在同一行场景中使用“ ==”和“包含”

时间:2019-07-25 15:33:05

标签: dsl karate

这是question noted here

的后续活动

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

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

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

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

我们在通用方案中实现架构验证,如下所示。我们可以根据环境轻松地使用v1Response / v2Response设置“响应”,并使用v1Schema / v2Schema设置“模式”。

* match response == schema

只要我们针对v1客户端测试v1服务器/针对v2客户端测试v2服务器,上述通用脚本都可以正常工作。但是,例如,当我们要测试向后兼容性时,我们不能重用同一场景 针对v1客户端的v2服务器。在这种情况下

* match response (actually v2Response) == schema (actually v1Schema) <--- will fail

因此,为了使其工作并进行向后兼容性测试,我还想使用空手道的“包含”功能,例如

* match response (actually v2Response) contains schema (actually v1Schema) <--- will pass

但是,为了使我的方案保持通用,目前无法执行任一操作

  1. 使用两个== /包含在同一行脚本中,如下所示
    • serverVersion == clientVersion? (匹配响应==模式):(匹配响应包含模式)

OR

  1. 使用一些标志,如下所示

    • 匹配响应SOMEFLAG模式

根据我们正在测试的环境,可以在karate-config.js中将SOMEFLAG设置为“ ==”或“ contains”。

编辑

在上面的示例中,我要做的只是测试以下应通过的情况

* match v1Response == v1Schema
* match v2Response == v2Schema 
* match v2Response contains v1Schema

使用以下通用行

* match response == schema <--- can it possibly be solved using above suggested solutions ?

1 个答案:

答案 0 :(得分:2)

由于某种原因,您认为破解match子句是解决此问题的唯一方法。请保持开放的态度,在这里,您去:

* 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 == schemas[env]

* def env = 'v2'
* def response = { id: "2", name: "awesome", value: "karate" }
* match response == schemas[env]

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

最后一行尽可能通用。