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

孤者浪人 提交于 2019-12-24 14:02:39

问题


This question is resulting from a previous question here

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" }

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

using a single generic line as follows

* 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.

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


回答1:


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)


来源:https://stackoverflow.com/questions/57224642/best-way-to-do-karate-match-using-and-contains-using-generic-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!