Postman - 断言 JSON 响应中有重复部分的位置中有、断言、位置、部分

2023-09-07 11:07:08 作者:听风说往事

这是我的 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)
        } 
    })
})
 
精彩推荐
图片推荐