如何在空手道中验证多个输入的响应

How to validate response with multiple inputs in karate

我正在尝试通过使用空手道框架提供多个输入来验证响应。以下是示例功能文件。

 Scenario Outline: response validation
    Given url 'urls?xyz=[<value>]'
    When method get
    Then status <status>
    And match response == [{abc:'<response>'},{pqr:'<response1>'}]

Examples:
      | value  | status | response | response1          |
      |      3 |    200 |        3 | null               |
      | *      |    400 |          | Invalid xyz        |
      |     65 |    200 |          |                    |
      | &^%^&% |    400 |          | Invalid xyz        |

但无法同时验证两个条件,其中一个参数将始终为空 'abc' 或 'pqr'。下面是我遇到的异常。

12:28:11.204 [pool-1-thread-1] DEBUG com.intuit.karate - response time in milliseconds: 742
12:28:11.276 [pool-1-thread-1] ERROR c.i.k.cucumber.KarateJunitFormatter - failed feature: path.my
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.util.List
    at com.intuit.karate.Script.matchNestedObject(Script.java:969)
    at com.intuit.karate.Script.matchJsonPath(Script.java:871)
    at com.intuit.karate.Script.matchNamed(Script.java:597)
    at com.intuit.karate.StepDefs.matchNamed(StepDefs.java:463)
    at com.intuit.karate.StepDefs.matchEquals(StepDefs.java:453)
    at ✽.And match response == [{abc:'3'},{pqr:''}](path/my.feature:27)

其实我建议你看一遍这个例子,它会给你很多想法并且有不同的方法来解决这个问题:dynamic-params.feature。例如,您可能希望使用 params 关键字而不是将查询参数连接到 URL.

根据我的经验,在 Examples: table 中放置一个 JSON 片段更简单,像这样:

And match response == <expected>

Examples:
  | value  | status | expected                      |
  |      3 |    200 | [{abc:3},{pqr:null}]          |
  | *      |    400 | [{pqr:'Invalid xyz'}]         |