如何在空手道框架中检查数组的长度?

问题描述 投票:0回答:2

我对REST Api有2种不同的响应,如下所示

1. {"type":null,"title":"Request is invalid","status":"BAD_REQUEST","detail":"Please correct request and try again.","invalidParams":[{"name":"create.arg0.name","reason":"Name is required"}]}

2. {"type":null,"title":"Unicode char u0000 is not allowed","status":"BAD_REQUEST","detail":"Unicode char u0000 is not allowed"}

我想写一个条件,如果invalidParams存在,则比较数组的内容。如果不是,则invalidParams应该为null / undefined。

 Scenario Outline: Create Asset Model with missing name and status
    Given url modelsUrl
    And request somedata
    When method POST
    Then status 400
    Then assert response.type == null
    Then match response.status == 'BAD_REQUEST'
    Then match (response.invalidParams != undefined && response.invalidParams[0].reason contains <reason>) OR (response.invalidParams == undefined)

但是与空值/未定义的值和长度进行比较也不起作用。如何在空手道中处理这种情况?普通的Javascript无法正常工作。

karate cucumber-java
2个回答
1
投票

要检查成员是否存在或检查数组的长度,应使用assert,而不是match。我也建议您将您的最后一个语句分成多个断言以提高透明度。

这里有一些示例,给出了一个名为'arr'的数组的响应:

检查数组长度

...
And assert response.arr.length == 1
...

检查数组是否存在

...
And assert response.arr != null
...

检查数组不存在

...
And assert response.arr == null
...

参考:https://intuit.github.io/karate/#payload-assertions


0
投票

基于riiich共享的链接,推荐的方法是对数组长度= 2执行以下操作:>

匹配响应=='#[2]'

© www.soinside.com 2019 - 2024. All rights reserved.