我与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"}
我想编写一个condion,如果invalidParams
在Respose中存在,请比较数组的内容。如果不是,则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)
,但与null/不污染的&amp;长度也无法正常工作。如何在空手道中处理这种情况?正常的JavaScript无法正常工作。
检查成员是否存在或检查数组的长度,应使用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
基于RIIICH共享的链接,推荐的方法是为阵列长度= 2
做以下方法匹配响应=='#[2]'