arrays - 无法循环浏览我的邮递员回复
问题描述
我有这个来自邮递员的 json 响应,如果数组中的键“值”< 50,我想编写一个测试以返回失败。
一旦条件不满足,它将遍历数组,它会失败
我试过这个
pm.test('Matches value', () => {
_.each(pm.response.json(), (arrItem) => {
if (arrItem.persID === 'personID_2') {
throw new Error(`Array contains ${arrItem.persID}`)
}
})
});
我的回复
{
"groups": [
{
"title": "Maids",
"subTitle": null,
"description": null,
"featured": false,
"items": [
{
"id": "1",
"title": "AA",
"subTitle": "AA",
"thumbnail": "AA",
"priceStartingAt": {
"value": 50,
"baseCurrency": "USD",
"exchangeEnabled": true,
"exchangeRates": {
"aed": 3.672973
}
},
"categories": [
"Activity"
]
},
{
"id": "2",
"title": "BB",
"subTitle": "BB",
"thumbnail": "BB",
"priceStartingAt": {
"value": 20.01,
"baseCurrency": "USD",
"exchangeEnabled": true,
"exchangeRates": {
"aed": 3.672973
}
},
"categories": [
"Activity"
]
}
]
}
]
在这种情况下,测试应该失败,因为第二个数组中的值为 20.01
解决方案
我不确定您从哪里复制了该代码,但它永远不会起作用,因为所有引用都与不同的响应主体相关。
为了保持相同的约定并throw new Error
在那里你可以这样做:
pm.test('Value is not below 50', () => {
_.each(pm.response.json().groups[0].items, (arrItem) => {
if (arrItem.priceStartingAt.value < 50) {
throw new Error(`Array contains ${arrItem.priceStartingAt.value}`)
}
})
});
或者你可以检查项目是否不在下面50
这样。
pm.test('Value is not below 50', () => {
_.each(pm.response.json().groups[0].items, (arrItem) => {
pm.expect(arrItem.priceStartingAt.value).to.not.be.below(50)
})
});
推荐阅读
- ios - 我可以在不使用 ios14 上的 xcode12 调用 AppTrackingTransparency 框架的情况下获得 idfa
- c# - 是否可以将 System.ValueTuple 4.5.0 安装到 TFS 2015 服务器上?
- python - 寻找一般建议,如何在 python 中抓取具有动态和交互式图形/表格的网站?
- excel - 将所有字段作为 Max 添加到数据透视表中
- python - 使用 Python 汇总字典列表中的值 -
- android - 如何使我的 ListView 在颤动中可滚动?
- ruby-on-rails - 如何保存具有虚拟属性的活动记录列表并验证该虚拟属性
- react-native - 我的 React Native 应用程序上的 Android Intent 重定向漏洞
- python - ctypes.windll.kernel32.SetConsoleTitleW 的 Linux 等价物是什么
- javascript - Javascript 输入值未定义