[英]How to check whether one node in API response has same value under all object using postman tests?
Suppose a API request fetches a users id, email address and their designated role. 假设API请求提取用户ID,电子邮件地址及其指定的角色。 Sample API Request below:
以下示例API请求:
GET: /v1/users HTTP/1.1
Content-Type: application/json
Authorization: bearer {access_token}
For the above request, the following is the response: 对于上述请求,以下是响应:
{
"content": [
{
"id": 1,
"email": "random@random.com",
"full_name": "AlbusDumbledore",
"role": "OWNER"
},
{
"id": 40,
"email": "random1@random1.com",
"role": "OWNER"
}
],
"last": false,
"total_elements": 2,
"total_pages": 1,
"sort": null,
"first": true,
"number_of_elements": 2,
"size": 20,
"number": 0
}
Now, what will be the test in postman to make sure that all the returned values under role node is equal to OWNER? 现在,postman中的测试将确保角色节点下的所有返回值等于OWNER?
You could add something like a loop to check for it? 你可以添加类似循环的东西来检查它吗?
pm.test("Check role equals OWNER", () => {
var jsonData = pm.response.json()
for (i = 0; i < jsonData.content.length; i++) {
pm.expect(jsonData.content[i].role).to.equal('OWNER')
}
})
This should work to check the value of each role
property, if the schema response you posted is correct. 如果您发布的架构响应正确,这应该可以检查每个
role
属性的值。
I changed one of the values and ran the test again to show you it working - This will show the failure in Postman if the property is not equal to 'OWNER'. 我更改了其中一个值并再次运行测试以显示它正常工作 - 如果属性不等于'OWNER',这将显示Postman中的失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.