无法遍历我的邮递员回复

时间:2019-07-30 21:46:44

标签: arrays postman

我从邮递员那里得到了这个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

1 个答案:

答案 0 :(得分:0)

我不确定您从何处复制了该代码,但是由于所有引用都与一个不同的响应主体相关,因此该代码永远无法工作。

要保持相同的约定并在其中放置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)
    })
});