gpt4 book ai didi

javascript - postman : How to assert all array elements exist in other array?

转载 作者:行者123 更新时间:2023-11-28 17:02:20 24 4
gpt4 key购买 nike

我断言 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) ]

尝试3 -

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

错误 -

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

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

最佳答案

Postman 默认情况下将 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

});

关于javascript - postman : How to assert all array elements exist in other array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57006246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com