【问题标题】:Postman Test. Looping through an array of nested objects to find an empty value邮递员测试。循环遍历嵌套对象数组以查找空值
【发布时间】:2021-02-24 17:36:45
【问题描述】:

我想请您帮忙/解决方案。 当我必须确保每个联系人都包含数组“phone_numbers”和“email”并获得一些值(不是空数组或值)时,我进行了测试。 我知道有一些测试语法,例如:

pm.test("Check for empty array's values", () => {
pm.expect(jsonData.contacts[1].phone_numbers).to.not.eql([]);
});

问题是我不想一一检查,因为有时很大。 我很确定有一个简单的解决方案。 我注意到有些联系人包含空的电子邮件、电话号码或两者兼有。例如"id": "id": 33333564 我以后不能允许了。

提前致谢:-)

{
  "contacts": [
    {
      "id": 11121211,
      "direct_link": "https://example.example",
      "first_name": "test1",
      "last_name": "test",
      "company_name": "test",
      "information": null,
      "is_shared": true,
      "created_at": 1582798926,
      "updated_at": 1582798926,
      "emails": [],
      "phone_numbers": [
        {
          "id": 60065270,
          "label": "Work",
          "value": "+33134567666"
        }
      ]
    },
    {
      "id": 22222222,
      "direct_link": "https://example.example",
      "first_name": null,
      "last_name": null,
      "company_name": null,
      "information": null,
      "is_shared": true,
      "created_at": 1583686067,
      "updated_at": 1583686067,
      "emails": [],
      "phone_numbers": [
        {
          "id": 22266444,
          "label": "Work",
          "value": "+33134567899"
        }
      ]
    },
    {
      "id": 33333564,
      "direct_link": "https://example.example",
      "first_name": "Jessica",
      "last_name": "Biel",
      "company_name": "N-Sync",
      "information": null,
      "is_shared": true,
      "created_at": 1583686086,
      "updated_at": 1583686086,
      "emails": [],
      "phone_numbers": []
    },
    {
      "id": 45678901,
      "direct_link": "https://example.example",
      "first_name": null,
      "last_name": null,
      "company_name": null,
      "information": null,
      "is_shared": true,
      "created_at": 1583686105,
      "updated_at": 1583686105,
      "emails": [],
      "phone_numbers": [
        {
          "id": 22266444,
          "label": "Work",
          "value": "+33134567333"
        }
      ]
    },
    {
      "id": 56789123,
      "direct_link": "https://example.example",
      "first_name": "Jacky",
      "last_name": "Rowland",
      "company_name": "Test Company1",
      "information": "",
      "is_shared": true,
      "created_at": 1583745888,
      "updated_at": 1608556499,
      "emails": [
        {
          "id": 76594398,
          "label": "Work",
          "value": "mandatory_field@example.com"
        }
      ],
      "phone_numbers": [
        {
          "id": 60650277,
          "label": "Mobile",
          "value": "+33652556777"
        }
      ]
    }
  ],
  "meta": {
    "count": 6,
    "total": 241,
    "current_page": 1,
    "per_page": 5,
    "next_page_link": "https://example.example",
    "previous_page_link": null
  }
}

【问题讨论】:

  • 这个解决方案有什么问题你得到什么错误?
  • 你的意思是我上面提到的解决方案 to.not.eql([])?它按预期工作。我只想运行一个测试,它遍历 JSON 中的每个数组,而不是单独测试每个对象数组
  • 答案有效吗?

标签: loops testing iteration postman


【解决方案1】:
a = pm.response.json()

 pm.test("Check for empty array's values", () => {
  
    checkEmpty = a.contacts.filter((a) => _.isEqual(a["phone_numbers"], []))
    pm.expect(checkEmpty,JSON.stringify(checkEmpty)).to.be.empty

    

});


pm.test("Check for e5mpty array's values", () => {

    checkEmpty = a.contacts.map((a) => pm.expect(a["phone_numbers"],JSON.stringify(a)).to.be.eq([]))

    

});

这里我们检查联系人数组中是否有任何元素的电话号码为空数组,如果是则返回。我们期望 array.filter 什么都不返回(这意味着不应该有任何空的电话号码)

在第二个测试中,我们使用 array.map 来检查是否有任何函数不为空

您可以使用上述任何测试函数,如果失败则打印数组内容的异常

【讨论】:

  • 非常感谢。我如何定义它?我收到一条未定义的错误消息。很抱歉问你,但我仍在学习,有时很难进行测试。..
  • a = pm.response.json()
  • 谢谢,这就是我为上面显示的示例所做的,并且效果很好。
  • 我在关注 JSOn 时遇到了困难:
  • a 将有完整的数据,我们正在使用过滤器或地图遍历联系人
猜你喜欢
  • 2023-04-01
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 2021-05-20
相关资源
最近更新 更多