如何使用动态字段名称断言空手道响应
How to assert Karate response with dynamic field name
API 响应示例:
{
"topic1": [
{"fieldName2": "value2",fieldName1": "value1"}
],
"topic2": [
{"fieldName2": "value2","fieldName1": "value1"},
{"fieldName2":"anotherValue2","fieldName1": "anotherValue1"}
]
}
这会起作用:
Then match response.topic1 == [{"fieldName2": "value2",fieldName1": "value1"}]
And match response == {"topic1":[{"fieldName2": "value2",fieldName1": "value1"}]}
但我需要将“topic1”放在这样的变量中,但无法找到使其工作的方法。
Then match response.TOPIC_NAME== [{"fieldName2": "value2",fieldName1": "value1"}]
也只尝试了 response.<TOPIC_NAME>
和 match response contains
消息,但也没有用。
* print "KAFKA_TOPIC: ", KAFKA_TOPIC
* print "response.KAFKA_TOPIC:", response.KAFKA_TOPIC
* print "response.topic1: ", response.topic1
日志:
11:47:47.946 [main] INFO com.intuit.karate - [print] KAFKA_TOPIC: topic1
11:47:47.949 [main] INFO com.intuit.karate - [print] response.KAFKA_TOPIC: null
11:47:47.951 [main] INFO com.intuit.karate - [print] response.topic1: [
{
"fieldName1": "value1",
"fieldName2": "value2",
"fieldName2": "value3"
}
]
现在尝试探索 https://github.com/karatelabs/karate#json-transforms
如果没有直接的方法。
您可以使用如下动态键访问 JSON:object[keyName]
。这只是幕后的 JS。这个例子应该让你的选择更清楚:
* def response =
"""
{
"topic1": [
{
"field1": "value1"
}
]
}
"""
* def key1 = 'topic1'
* def val1 = response[key1]
* match val1 == [{ field1: 'value1' }]
# to do this in one line
* match (response[key1]) == [{ field1: 'value1' }]
API 响应示例:
{
"topic1": [
{"fieldName2": "value2",fieldName1": "value1"}
],
"topic2": [
{"fieldName2": "value2","fieldName1": "value1"},
{"fieldName2":"anotherValue2","fieldName1": "anotherValue1"}
]
}
这会起作用:
Then match response.topic1 == [{"fieldName2": "value2",fieldName1": "value1"}]
And match response == {"topic1":[{"fieldName2": "value2",fieldName1": "value1"}]}
但我需要将“topic1”放在这样的变量中,但无法找到使其工作的方法。
Then match response.TOPIC_NAME== [{"fieldName2": "value2",fieldName1": "value1"}]
也只尝试了 response.<TOPIC_NAME>
和 match response contains
消息,但也没有用。
* print "KAFKA_TOPIC: ", KAFKA_TOPIC
* print "response.KAFKA_TOPIC:", response.KAFKA_TOPIC
* print "response.topic1: ", response.topic1
日志:
11:47:47.946 [main] INFO com.intuit.karate - [print] KAFKA_TOPIC: topic1
11:47:47.949 [main] INFO com.intuit.karate - [print] response.KAFKA_TOPIC: null
11:47:47.951 [main] INFO com.intuit.karate - [print] response.topic1: [
{
"fieldName1": "value1",
"fieldName2": "value2",
"fieldName2": "value3"
}
]
现在尝试探索 https://github.com/karatelabs/karate#json-transforms 如果没有直接的方法。
您可以使用如下动态键访问 JSON:object[keyName]
。这只是幕后的 JS。这个例子应该让你的选择更清楚:
* def response =
"""
{
"topic1": [
{
"field1": "value1"
}
]
}
"""
* def key1 = 'topic1'
* def val1 = response[key1]
* match val1 == [{ field1: 'value1' }]
# to do this in one line
* match (response[key1]) == [{ field1: 'value1' }]