邮递员 - 断言 JSON 响应中存在重复部分的位置 [英] Postman - asserting where there are duplicate sections in JSON response

查看:26
本文介绍了邮递员 - 断言 JSON 响应中存在重复部分的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 JSON 响应.我想编写一个测试,断言成本 = 1.000,其中提供者 = Apple.由于响应中重复了成​​本,我很挣扎.我该怎么办?谢谢.

<预><代码>[{"provider": "苹果",成本":1.000,活动":真实,总计":假},{"provider": "香蕉",成本":0.000,活动":真实,总计":假},{"provider": "葡萄",成本":0.000,活动":真实,总计":假}]

解决方案

您可以编写一个测试,循环遍历响应中的每个对象,如果对象包含 Apple 值,则检查 costprovider 键:

让 jsonData = pm.response.json()pm.test('查看苹果的价格', () => {_.each(jsonData, (item) => {if(item.provider === '苹果') {pm.expect(item.cost).to.equal(1.000)}})})

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
    }
]

解决方案

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)
        } 
    })
})

这篇关于邮递员 - 断言 JSON 响应中存在重复部分的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆