当有多个键响应时,如何根据条件检查空手道中的特定值
How to check a particular value in karate on basis of condition when there are more than one key in response
x.details[0].user
中存在多个键,那么如何在这种情况下进行比较。它在只有一个键的情况下工作正常,而当有多个键时它会失败并出现错误,因为用户有多个键。
请指导
* def array =
"""
{
"type": "1",
"array": 2,
"details": [
{
"path": "path",
"user": {
"school": {
"name": [
{
"value": "this is school",
"codeable": {
"details": [
{
"hello": "yty",
"condition": "check1"
}
],
"text": "123S"
}
}
]
},
"sample": "test1",
"id": "22222"
},
"data": {
"check": "abc"
}
},
{
"path": "path",
"user": {
"school": {
"name": [
{
"value": "this is school",
"codeable": {
"details": [
{
"hello": "def",
"condition": "check2"
}
],
"text": "123O"
}
}
]
},
"sample": "test",
"id": "11111"
},
"data": {
"check": "xyz"
}
}
]
}
"""
* def lookup = { 'check1': 'yty', 'check2': 'def' }
* match each array.details contains { user: { school: { name[0]: { codeable: { details[0]: { hello: '#(lookup[_$.user.school.name[0].codeable.details[0].condition])' } } } } } }
我尝试了多种方法,但不确定在返回多个密钥时如何使它工作。
先说这个。拜托,请 不要试图在测试中“太聪明”。请阅读:
只需进行简单的测试,测试特定的、可预测的响应,然后就此打住。最大复杂度应该是match contains
,就是这样。在我看来,您尝试做的事情是在浪费时间,并且只会给将来试图维护这些测试的任何人带来悲伤。
也就是说,我在这里给你一个完全不同的方法。有几种不同的方法,这只是其中一种。在这里,我们逐个解决问题,而不是尝试编写大量匹配项。
* def allXyz = array.details.filter(x => x.data.check == 'xyz')
* match each allXyz..details == [{ hello: 'def', condition: 'check2' }]
你应该能够扩展它来做你想要的所有奇怪的断言。如果你想知道 allXyz..details
是做什么的,你可以这样打印它:
* def temp = $allXyz..details
* print temp
x.details[0].user
中存在多个键,那么如何在这种情况下进行比较。它在只有一个键的情况下工作正常,而当有多个键时它会失败并出现错误,因为用户有多个键。
请指导
* def array =
"""
{
"type": "1",
"array": 2,
"details": [
{
"path": "path",
"user": {
"school": {
"name": [
{
"value": "this is school",
"codeable": {
"details": [
{
"hello": "yty",
"condition": "check1"
}
],
"text": "123S"
}
}
]
},
"sample": "test1",
"id": "22222"
},
"data": {
"check": "abc"
}
},
{
"path": "path",
"user": {
"school": {
"name": [
{
"value": "this is school",
"codeable": {
"details": [
{
"hello": "def",
"condition": "check2"
}
],
"text": "123O"
}
}
]
},
"sample": "test",
"id": "11111"
},
"data": {
"check": "xyz"
}
}
]
}
"""
* def lookup = { 'check1': 'yty', 'check2': 'def' }
* match each array.details contains { user: { school: { name[0]: { codeable: { details[0]: { hello: '#(lookup[_$.user.school.name[0].codeable.details[0].condition])' } } } } } }
我尝试了多种方法,但不确定在返回多个密钥时如何使它工作。
先说这个。拜托,请 不要试图在测试中“太聪明”。请阅读:
只需进行简单的测试,测试特定的、可预测的响应,然后就此打住。最大复杂度应该是match contains
,就是这样。在我看来,您尝试做的事情是在浪费时间,并且只会给将来试图维护这些测试的任何人带来悲伤。
也就是说,我在这里给你一个完全不同的方法。有几种不同的方法,这只是其中一种。在这里,我们逐个解决问题,而不是尝试编写大量匹配项。
* def allXyz = array.details.filter(x => x.data.check == 'xyz')
* match each allXyz..details == [{ hello: 'def', condition: 'check2' }]
你应该能够扩展它来做你想要的所有奇怪的断言。如果你想知道 allXyz..details
是做什么的,你可以这样打印它:
* def temp = $allXyz..details
* print temp