最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Postman : How to assert all array elements exist in other array? - Stack Overflow

programmeradmin5浏览0评论

I'm asserting for elements in array1 does exist in array2 or not, below are the array samples,

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
}]

code I tried -

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

    }

error I get -

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

EDIT after trying another attempt2 -

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

error -

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

attempt3 -

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

error -

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

what I'm doing wrong?. what I want is if any one element in array2 is not in array1 it should fail.
Thanks

I'm asserting for elements in array1 does exist in array2 or not, below are the array samples,

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
}]

code I tried -

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

    }

error I get -

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

EDIT after trying another attempt2 -

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

error -

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

attempt3 -

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

error -

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

what I'm doing wrong?. what I want is if any one element in array2 is not in array1 it should fail.
Thanks

Share Improve this question edited Jul 13, 2019 at 21:53 Dev asked Jul 12, 2019 at 11:37 DevDev 2,8132 gold badges24 silver badges37 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

Chai assertion Library is included by Postman by default in its application. So you need to use the to.deep.equal. It will pare nested arrays as well as nested objects.

SOLUTION:

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

});

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' 
  }
})

what I want is if any one element in array2 is not in array1 it should fail

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, pares 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

发布评论

评论列表(0)

  1. 暂无评论