在Karate中的AND条件

huangapple go评论71阅读模式
英文:

AND condition in Karate

问题

[
{
"X": "shop",
"description": "ABC",
"Y": {
"Z": {
"T1": "1",
"T2": "2"
}
}
}]

现在需要在Karate中编写一个条件,如果(T1 == '1' && T2 == '2')则断言“X”=“shop”。如何在一行或简短的方式中实现这一目标?已尝试各种方法但未成功。

英文:

[
{
“X”: “shop”,
"description": “ABC”,
“Y”: {
“Z”: {
“T1”: “1”,
“T2”: “2”}
}
}]

Now need to write a condition in karate that if (T1 == ‘1’ && T2 == ‘2’) then assert that “X” = “shop”
How can we achieve this in a single line or in short way

Tried various methods but did not work

答案1

得分: 1

这确实是一个非常复杂的JSON,所以我会尝试"预处理"它以使其更简单。

以下是其中一种可能的解决方案之一。请使用提示来确定适合您的解决方案:

  • def response = [{ X: 'shop', Y: { Z: { T1: '1', T2: '2' } } }]
  • def data = response.map(x => ({ X: x.X, T1: x.Y.Z.T1, T2: x.Y.Z.T2 }))
  • def good = data.filter(x => x.T1 == '1' && x.T2 == '2' && x.X == 'shop')
  • match data == good

更多提示,请参考这里:https://stackoverflow.com/a/62567262/143475

英文:

This is certainly very complicated JSON, so I would try to "pre process" it to be simpler.

Here is one possible solution out of many. Please use the hints to arrive at what works for you:

* def response = [{ X: 'shop', Y: { Z: { T1: '1', T2: '2' } } }]
* def data = response.map(x => ({ X: x.X, T1: x.Y.Z.T1, T2: x.Y.Z.T2 }))
* def good = data.filter(x => x.T1 == '1' && x.T2 == '2' && x.X == 'shop')
* match data == good

Refer here for more hints: https://stackoverflow.com/a/62567262/143475

huangapple
  • 本文由 发表于 2023年8月4日 05:48:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定