postman - 如何在 Postman 中循环函数
问题描述
我试图编写测试每个集合中每个元素的存在。例如:是否familyName
存在于每个集合中,或者firstName
存在于每个集合中,等等。
当我尝试to.have.property
在循环中使用函数时(如下所示),我收到消息don't use function inside the loop
。
var jsonData = pm.response.json();
totalNumber = jsonData.length;
while (i < totalNumber) {
i=0;
pm.test('familyName is present in the response', function() {
pm.expect(jsonData[i]).to.have.property('agentSSN');
i++;
}
)};
响应样本:
[
{
"familyName": "123",
"firstName": "tester2",
"middleName": "",
"lastName": "test ",
"ContactNumber1": "",
"ContactNumber2": ""
},
{
"familyName": "123",
"firstName": "tester1",
"middleName": "",
"lastName": "test2",
"ContactNumber1": "",
"ContactNumber2": ""
}
]
解决方案
你可以试试这个:
pm.test('familyName is present in the response', () => {
_.each(pm.response.json(), (item) => {
pm.expect(item).to.have.property('agentSSN');
})
})
它将遍历响应并检查它property
是否在对象中。
推荐阅读
- arrays - 在java中旋转数组的一部分
- javascript - 单击两次时,JQuery 未打开已打开的窗口
- python - Matplotlib 在动画刷新后连接数据的结尾
- linux - 重定位 R_X86_64_PC32 对符号 _ZTISt13runtime_error@@GLIBCXX_3.4 在制作共享对象时不能使用;使用 -fPIC 重新编译
- java - 如何在 Spring Boot 中获取请求的 url
- java - 在子进程中的java应用程序中提交spark作业,子进程永远不会退出
- html - Django 不从静态文件夹提供 CSS 代码
- javascript - 一种防止在 puppeteer 实例中打开开发工具的方法
- php - Left Join Doctrine 查询生成器
- javascript - 在 angularjs 中使用 $event