邮递员:如何断言所有数组元素都存在于另一个数组中?

时间:2019-07-12 11:37:41

标签: javascript arrays json postman postman-testcase

我要断定array1中的元素是否确实存在于array2中,下面是数组样本,

var array1 = [
        {
            "name": "appe",
            "loc": "war",
            "order": 5,
            "neck": "NO",
            "end": false
        },
        {
            "name": "con",
            "loc": "conve",
            "order": 15,
            "neck": "NO",
            "end": true
        }]

var array2 = [{"neck":"NO"
"end":true,
"loc":"conve",
"name":"con",
"order":15
}]

我尝试过的代码-

const lodash = require("lodash");
    for(var i = 0; i < array2.length; i++ )
    {
            tests['response json contain Data'] = lodash._.has(points, array2[i]);
            //pm.expect(array2[i]).to.be.oneOf(array1);

    }

我得到的错误-

response json contain Data | AssertionError: expected false to be truthy

编辑 尝试另一次尝试后2-

pm.expect(array2[i]).to.be.oneOf(array1);

错误-

AssertionError | expected { Object (name, loc, ...) } to be one of [ Array(20) ]

attempt3-

pm.expect(array1).to.deep.equal(array2);

错误-

AssertionError | expected [ Array(20) ] to deeply equal [ Array(18) ]

我做错了什么?我想要的是,如果array2中的任何一个元素不在array1中,它应该会失败。
谢谢

3 个答案:

答案 0 :(得分:1)

邮递员默认将

Chai断言库包含在其应用程序中。因此,您需要使用to.deep.equal。它将比较嵌套数组和嵌套对象。

解决方案:

pm.test("verify two objects", function () {
  var array1 = [{
    "name": "appe",
    "loc": "war",
    "order": 5,
    "neck": "NO",
    "end": false
  },
  {
    "name": "con",
    "loc": "conve",
    "order": 15,
    "neck": "NO",
    "end": true
  }];

  var array2 = [{
    "neck": "NO",
    "end": true,
    "loc": "conve",
    "name": "con",
    "order": 15
  }, 
  {
    "neck": "NOo",
    "end": true,
    "loc": "conve",
    "name": "con",
    "order": 15
  }];

  pm.expect(array1).to.deep.equal(array2); // Failed
  pm.expect(array1).to.deep.equal(array1); // Passed

});

答案 1 :(得分:0)

var array1 = [{
    "name": "appe",
    "loc": "war",
    "order": 5,
    "neck": "NO",
    "end": false
  },
  {
    "name": "con",
    "loc": "conve",
    "order": 15,
    "neck": "NO",
    "end": true
  }
]

var array2 = [{
  "neck": "NO",
  "end": true,
  "loc": "conve",
  "name": "con",
  "order": 15
}]

array2.forEach( item => {
  if ( !array1.includes(item)){
  throw 'doesn\'t include' 
  }
})

答案 2 :(得分:0)

  

我想要的是,如果array2中的任何一个元素不在array1中,它应该失败

var array1 = [{
    "name": "appe",
    "loc": "war",
    "order": 5,
    "neck": "NO",
    "end": false
  },
  {
    "name": "con",
    "loc": "conve",
    "order": 15,
    "neck": "NO",
    "end": true
  }
]

var array2 = [{
  "neck": "NO",
  "end": true,
  "loc": "conve",
  "name": "con",
  "order": 15
}, {
  "neck": "NOo",
  "end": true,
  "loc": "conve",
  "name": "con",
  "order": 15
}];

// Finds at least one object on array2 which is not in array1.
// The function some finds at least one according to the 
// result of findIndex which is using a handler who executes 
// the function every.
// The function every, basically, compares every key-value 
// between array2 and array1.
let result = array2.some(o => array1.findIndex(ao => Object.entries(o).every(([key, value]) => ao[key] === value)) === -1);

console.log(result); // Returns true because one element is not in array1