【问题标题】:Postman AssertionError: expected [ Array(9) ] to have property 'EID'Postman AssertionError:预期 [ Array(9) ] 具有属性“EID”
【发布时间】:2020-04-04 16:21:31
【问题描述】:

响应正文:

[
   {
      "fleets":[
         "efgh"
      ],
      "cellVersion":"FWA 1.2",
      "mainVersion":null,
      "deviceConfiguration":{
         "id":"af405fa16e25",
         "name":"Pal_testBed_1",
         "gps":{
            "position":30
         },
         "sensorsMap":[
            {
               "address":"29",
               "signalId":3
            },
            {
               "address":"29",
               "signalId":4
            }
         ],
         "protocol":"generic"
      },
      "simCard":{
         "id":"d0"
      },
      "status":"activated",
      "owner":"4f99",
      "name":"Test",
      "EID":"simulator3",
      "id":"5678"
   }
]

邮递员测试用例:

pm.test("Check if body has devices list", function () {
    var jsonData = pm.response.json();

    pm.expect(jsonData).to.have.property("EID");
})

错误:

检查body是否有设备列表| AssertionError: 预期 [ Array(9) ] 具有属性 'EID'

【问题讨论】:

  • 您的 JSON 数据是一个数组,您可以检查该数组中的每个元素。对于第一个元素,您可以使用 pm.expect(jsonData[0]).to.have.property("EID"); })
  • 如果要检查:pm.expect(jsonData[0]).to.have.property("fleets","EID","gps","signalId");错误:检查主体是否有设备列表并获取第一个设备 | AssertionError: gps: 预期 { Object (fleets, cellFirmwareVersion, ...) } 具有 'EID' 的属性 'fleets',但得到 [ Array(14) ]
  • 能否请您发布原始代码,而不是它的图像?使人们更容易在模拟等本地运行它。
  • [{"fleets": ["efgh"],"cellVersion": "FWA 1.2","mainVersion": null,"deviceConfiguration": {"id": "af405fa16e25","name ": "Pal_testBed_1","gps": {"position": 30,},"sensorsMap": [{"address": "29","signalId": 3},{"address": "29"," signalId": 4}],"protocol": "generic",},"simCard": {"id": "d0",},"status": "activated","owner": "4f99","name ": "Test","EID": "simulator3","id": "5678"}] 修改了一个数组以适应限制

标签: javascript testing automation postman assertion


【解决方案1】:

您可以添加一些基本的东西来循环遍历array 中的每个object 以检查这些属性:

pm.test("Check if body has devices list", function () {
    _.each(pm.response.json(), (item) => {
        pm.expect(item).to.have.property("EID");
        pm.expect(item).to.have.property("fleets");
        pm.expect(item).to.have.property("gps");
        pm.expect(item).to.have.property("signalId");
    })
})

这是一个如何检查它的示例,但我确信有一种更有效的方法可以实现同样的目的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 2023-02-03
    • 2020-07-11
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多