【发布时间】:2019-03-30 17:35:00
【问题描述】:
无论响应中的所有数据返回 id、firstname、lastname 等结果的数量如何,如何在 postman 中进行验证
下面是响应的样子:
[
{
"id": 1,
"first_name": "Sebastian",
"last_name": "Eschweiler",
"email": "sebastian@codingthesmartway.com"
},
{
"first_name": "Sebastian",
"last_name": "Eschweiler",
"email": "sebastian@codingthesmartway.com",
"id": 4
},
{
"id": 5,
"first_name": "Sebastian",
"last_name": "Eschweiler",
"email": "sebastian@codingthesmartway.com"
},
{
"first_name": "Sebastian",
"last_name": "Eschweiler",
"email": "sebastian@codingthesmartway.com",
"id": 8
},
{
"id": 9,
"first_name": "Sebastian",
"last_name": "Eschweiler",
"email": "sebastian@codingthesmartway.com"
},
{
"first_name": "Sebastian",
"last_name": "Eschweiler",
"email": "sebastian@codingthesmartway.com",
"id": 12
}
]
我想验证两件事:
1) 响应返回 id、first_name、last_name、email
2) 无论只有一个结果还是 100,所有 first_name 都等于“Sebastian”
这是我尝试过的,但它只适用于一个结果:
const jsonData = pm.response.json();
pm.test('Has data', function() {
pm.expect(jsonData).to.have.property('first_name');
pm.expect(jsonData).to.have.property('last_name');
pm.expect(jsonData).to.have.property('email');
pm.expect(jsonData).to.have.property('id');
});
【问题讨论】:
-
您是否尝试过检查返回的 JSON 是 JSON 对象还是 JSON 数组?如果它是一个数组,您可能需要将您的 has 数据检查放入 forEach/for 循环中。
标签: javascript node.js postman postman-collection-runner