断言JSON数组包含一个属性

时间:2017-07-24 08:11:30

标签: javascript postman chai web-api-testing

我有一个设备商店项目,允许添加/删除/编辑设备,我试图在邮递员中测试,在POST后,将设备添加到列表中,设备可以在我的响应正文中找到。我正在使用Postman BDD和Chai sintax

[
{
    "deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
    "name": "Huawei",
    "alias": "electronics",
    "quantity": 10,
    "price": 200,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "90807800-c66c-46ec-ae46-687464e62797",
    "name": "Pixel 2",
    "alias": "electronics",
    "quantity": 10,
    "price": 300,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
    "name": "SONYk",
    "alias": "electronicsm",
    "quantity": 122,
    "price": 2222,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
    "name": "ASUS1",
    "alias": "electronics",
    "quantity": 1222,
    "price": 2222,
    "categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
    "links": []
}

这是我的回答,因为可以看到它返回一个JSON数组,我正在寻找一种优雅的方法来断言具有属性“Pixel 2”的设备在数组中。我对任何其他可以帮助我实现此功能的javascript库都持开放态度。

5 个答案:

答案 0 :(得分:2)

您可以使用接受回调提供的功能的find方法。



var array=[
{
    "deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
    "name": "Huawei",
    "alias": "electronics",
    "quantity": 10,
    "price": 200,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "90807800-c66c-46ec-ae46-687464e62797",
    "name": "Pixel 2",
    "alias": "electronics",
    "quantity": 10,
    "price": 300,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
    "name": "SONYk",
    "alias": "electronicsm",
    "quantity": 122,
    "price": 2222,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
    "name": "ASUS1",
    "alias": "electronics",
    "quantity": 1222,
    "price": 2222,
    "categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
    "links": []
}];
var item=array.find(function(item){
  return item.name=="Pixel 2";
});
console.log(item);
console.log("Exists: "+item!=undefined);




另一种方法是使用includes函数。



var array=[
{
    "deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
    "name": "Huawei",
    "alias": "electronics",
    "quantity": 10,
    "price": 200,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "90807800-c66c-46ec-ae46-687464e62797",
    "name": "Pixel 2",
    "alias": "electronics",
    "quantity": 10,
    "price": 300,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
    "name": "SONYk",
    "alias": "electronicsm",
    "quantity": 122,
    "price": 2222,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
    "name": "ASUS1",
    "alias": "electronics",
    "quantity": 1222,
    "price": 2222,
    "categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
    "links": []
}];
var exists=array.map(function(item){
  return item.name;
}).includes("Pixel 2");
console.log(exists);




答案 1 :(得分:1)

您可以使用Array#some

response.some( o => o.name == "Pixel 2" )

结果是布尔值。

答案 2 :(得分:1)

不需要图书馆。您可以使用Array.prototype.some

if (response.some(i => i.name == 'Pixel 2')) {
  // ...
}

答案 3 :(得分:0)

使用Array#filterArray#length一起查找您的项目是否存在



const array = [{
        "deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
        "name": "Huawei",
        "alias": "electronics",
        "quantity": 10,
        "price": 200,
        "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
        "links": []
    },
    {
        "deviceid": "90807800-c66c-46ec-ae46-687464e62797",
        "name": "Pixel 2",
        "alias": "electronics",
        "quantity": 10,
        "price": 300,
        "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
        "links": []
    },
    {
        "deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
        "name": "SONYk",
        "alias": "electronicsm",
        "quantity": 122,
        "price": 2222,
        "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
        "links": []
    },
    {
        "deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
        "name": "ASUS1",
        "alias": "electronics",
        "quantity": 1222,
        "price": 2222,
        "categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
        "links": []
    }
];

const hasPixel2 = array.filter(item => item.name === 'Pixel 2').length > 0;
console.log(hasPixel2);




答案 4 :(得分:0)

Array#Filter

var array=[
{
    "deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
    "name": "Huawei",
    "alias": "electronics",
    "quantity": 10,
    "price": 200,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "90807800-c66c-46ec-ae46-687464e62797",
    "name": "Pixel 2",
    "alias": "electronics",
    "quantity": 10,
    "price": 300,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
    "name": "SONYk",
    "alias": "electronicsm",
    "quantity": 122,
    "price": 2222,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
    "name": "ASUS1",
    "alias": "electronics",
    "quantity": 1222,
    "price": 2222,
    "categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
    "links": []
}];
var item = array.filter(item => item.name === "Pixel 2");
console.log(item);