我对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无法正常工作。
答案 0 :(得分:1)
要检查成员是否存在或检查数组的长度,应使用assert
而不是match
。我也建议您将您的最后一个语句分成多个断言以保持透明度。
以下是一些示例,给出了名为“ arr”的数组的响应:
检查数组长度
...
And assert response.arr.length == 1
...
检查数组是否存在
...
And assert response.arr != null
...
缺少检查数组
...
And assert response.arr == null
...
答案 1 :(得分:0)
基于riiich共享的链接,建议的方法是对数组长度= 2进行以下操作
匹配响应=='#[2]'