Javascript Postman AssertionError:预期[Array(9)]具有属性';EID&x27;

Javascript Postman AssertionError:预期[Array(9)]具有属性';EID&x27;,javascript,testing,automation,postman,assertion,Javascript,Testing,Automation,Postman,Assertion,答复机构: [ { "fleets":[ "efgh" ], "cellVersion":"FWA 1.2", "mainVersion":null, "deviceConfiguration":{ "id":"af405fa16e25", "name":"Pal_testBed_1", "gps":{ "position":30

答复机构:

[
   {
      "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”

您可以添加一些基本内容,以循环通过
数组中的每个
对象来检查这些属性:

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");
    })
})
这是一个如何检查的示例,但我相信有一种更有效的方法可以实现同样的效果。

您可以添加一些基本内容,以循环通过
数组中的每个
对象来检查这些属性:

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

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

您的JSON数据是一个数组,您可以检查该数组中的每个元素。对于第一个元素,可以使用
pm.expect(jsonData[0]).to.have.property(“EID”);}如果要检查:pm.expect(jsonData[0]).to.have.property(“震源组”、“EID”、“gps”、“信号ID”);错误:检查body是否有设备列表并抓取第一个设备|断言错误:gps:预期{Object(fleets,cellFirmwareVersion,…}的属性'fleets'为'EID',但得到了[Array(14)]您能发布原始代码吗,而不是它的图像?使人们更容易在模拟设备中本地运行它。{“车队”:[“efgh”],“cellVersion”:“FWA 1.2”,“mainVersion”:null,“deviceConfiguration”:{“id”:“af405fa16e25”,“名称”:“Pal_试验台1”,“gps”:{“位置”:30,},“sensorsMap”:[{“地址”:“29”,“信号id”:3},{“地址”:“29”,“信号id”:4}],“协议”:“通用”,},“simCard”:{“id”:“d0,“status:“activated”,“owner:“4f99”,“name:“Test”,“EID:“simulator3”,“id:“5678”}]已撤销一个数组以适应限制如果JSON数据是数组,则可以检查此数组中的每个元素。对于第一个元素,可以使用
pm.expect(jsonData[0]).to.have.property(“EID”);}
如果要检查:pm.expect(jsonData[0]).to.have.property(“fleets”、“EID”、“gps”、“signalId”);错误:检查主体是否有设备列表并抓取第一个设备|断言错误:gps:预期{Object(fleets,cellFirmwareVersion,…}的属性“fleets”为“EID”,但获得了[Array(14)]你可以发布原始代码,而不是它的图像吗?使人们更容易在模拟中本地运行它。[{“车队”:[“efgh”],“cellVersion”:“FWA 1.2”,“mainVersion”:null,“deviceConfiguration”:{“id”:“af405fa16e25”,“名称”:“Pal_测试床_1”,“gps”:{“位置”:30,},“sensorsMap”:[{“地址”:“29”,“信号id”:3},{“地址”:“29”,“signalId“:4}],“协议“:”通用“,”simCard“:{”id“:”d0“,”状态“:”已激活“,”所有者“:”4f99“,”名称“:”测试“,”EID“:”模拟器3“,”id“:”5678“}]撤销了一个数组以符合限制