空手道测试框架:仅包含多行JSON

时间:2019-04-24 21:44:35

标签: karate

我正在使用空手道测试框架。

此功能有效:

Given path '/endpoint'
When method GET
Then match response.list contains only { "field1": "value1", "field2": "value2"}

但是我不想在一行中编写整个JSON,所以我尝试了:

Given path '/endpoint'
When method GET
Then match response.list contains only
"""
{
  "field1": "value1",
  "field2": "value2"
}
"""

我收到此错误:

 Tests in error: 
  example.feature:11 - more than one step-definition method matched: match response.list contains only - [public void com.intuit.karate.StepActions.matchDocstring(java.lang.String,java.lang.String,java.lang.String) [response.list contains , only], public void com.intuit.karate.StepActions.match(java.lang.String,java.lang.String,java.lang.String) [response.list , contains,  only]]

有什么方法可以使用仅包含,也可以使用多行JSON?预先感谢

1 个答案:

答案 0 :(得分:2)

是的,这是一个解析器边缘情况,仅分为两个步骤:

Given def response = { list: { "field1": "value1", "field2": "value2" } }
And def expected =
"""
{ "field1": "value1", "field2": "value2"}
"""
Then match response.list contains only expected

请注意,contains only仅对JSON数组有意义,请仔细考虑。