Postman - 断言 JSON 响应中有重复部分的位置 [英] Postman - asserting where there are duplicate sections in JSON response
本文介绍了Postman - 断言 JSON 响应中有重复部分的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的 JSON 响应.我想编写一个断言成本 = 1.000 的测试,其中提供者 = Apple.由于成本在响应中重复,我很挣扎.我该怎么办?谢谢.
This is my JSON response. I am wanting to write a test that asserts that cost = 1.000 where provider = Apple. As cost is repeated in the response, I'm struggling. How would I go about this? Thanks.
[
{
"provider": "Apple",
"cost": 1.000,
"active": true,
"total": false
},
{
"provider": "Banana",
"cost": 0.000,
"active": true,
"total": false
},
{
"provider": "Grape",
"cost": 0.000,
"active": true,
"total": false
}
]
推荐答案
你可以编写一个循环遍历响应中每个对象的测试,检查 cost
值,如果对象包含 Apple 值provider
键:
You can write a test which loops through each object in the response, checks the cost
value if the object contains the Apple value in the provider
key:
let jsonData = pm.response.json()
pm.test('Check the price of the Apple', () => {
_.each(jsonData, (item) => {
if(item.provider === 'Apple') {
pm.expect(item.cost).to.equal(1.000)
}
})
})
这篇关于Postman - 断言 JSON 响应中有重复部分的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文