如何断言数组中的数组?
How to assert array in array?
有人可以帮我在 postman 中断言数组中的数组吗?
我有这个安心的代码:
"documents": [
{
"fileName": "file01_guid.pdf",
"documentType": "document",
"parentFiles": [
"file01.pdf"
]
},
{
"fileName": "file02_guid.pdf",
"documentType": "document",
"parentFiles": [
"file01.pdf"
]
}
我需要使用此方法断言“ParentFiles”数组:
var array = [];
var range = (json_response.data.documents).length
for (var i = 0; i < range; i++)
{
var file = json_response.data.documents[i].fileName
var type = json_response.data.documents[i].documentType;
array.push(file)
array.push(type)
}
所以我可以写这样的测试:
{
pm.expect(array).to.include("file01.pdf", "file01.pdf");
});
提前致谢
您可以通过简单地过滤和检查长度来验证 "file01.pdf"
是否存在。您还可以通过减少
更有效地构建您的 array
const json_response = {
"data": {
"documents": [{
"fileName": "file01_guid.pdf",
"documentType": "document",
"parentFiles": ["file01.pdf"]
}, {
"fileName": "file02_guid.pdf",
"documentType": "document",
"parentFiles": ["file01.pdf"]
}]
}
};
const array = json_response.data.documents.reduce((acc, doc) => {
const { fileName, documentType, parentFiles: [ pfName ] } = doc;
return [ ...acc, fileName, documentType, pfName ];
}, []);
console.log(array);
// Exactly two instances of "file01.pdf" exist.
console.log(array.filter(val => val === "file01.pdf").length === 2);
.as-console-wrapper { top: 0; max-height: 100% !important; }
如果你只是想比较数组,你可以尝试以下方法:
const json_response = {
"data": {
"documents": [{
"fileName": "file01_guid.pdf",
"documentType": "document",
"parentFiles": ["file01.pdf"]
}, {
"fileName": "file02_guid.pdf",
"documentType": "document",
"parentFiles": ["file01.pdf"]
}]
}
};
const allEqual = json_response.data.documents
.every(({ parentFiles: [ filename ] }) => filename === "file01.pdf");
console.log(allEqual);
.as-console-wrapper { top: 0; max-height: 100% !important; }
您可以使用 foreach 循环并比较值
//store expected files in order
array=["file01.pdf","file01.pdf"]
//now forEach on each element
json_response.data.documents.forEach( (elem,index,arr)=>{
pm.expect(elem.parentFiles[0]).to.be.eql(array[index])
})
如果你只是想比较两个数组那么:
pm.expect(_.isEqual(array,array2)).to.be.true
lodash isEqual 可用于比较两个对象,如数组
有人可以帮我在 postman 中断言数组中的数组吗?
我有这个安心的代码:
"documents": [
{
"fileName": "file01_guid.pdf",
"documentType": "document",
"parentFiles": [
"file01.pdf"
]
},
{
"fileName": "file02_guid.pdf",
"documentType": "document",
"parentFiles": [
"file01.pdf"
]
}
我需要使用此方法断言“ParentFiles”数组:
var array = [];
var range = (json_response.data.documents).length
for (var i = 0; i < range; i++)
{
var file = json_response.data.documents[i].fileName
var type = json_response.data.documents[i].documentType;
array.push(file)
array.push(type)
}
所以我可以写这样的测试:
{
pm.expect(array).to.include("file01.pdf", "file01.pdf");
});
提前致谢
您可以通过简单地过滤和检查长度来验证 "file01.pdf"
是否存在。您还可以通过减少
array
const json_response = {
"data": {
"documents": [{
"fileName": "file01_guid.pdf",
"documentType": "document",
"parentFiles": ["file01.pdf"]
}, {
"fileName": "file02_guid.pdf",
"documentType": "document",
"parentFiles": ["file01.pdf"]
}]
}
};
const array = json_response.data.documents.reduce((acc, doc) => {
const { fileName, documentType, parentFiles: [ pfName ] } = doc;
return [ ...acc, fileName, documentType, pfName ];
}, []);
console.log(array);
// Exactly two instances of "file01.pdf" exist.
console.log(array.filter(val => val === "file01.pdf").length === 2);
.as-console-wrapper { top: 0; max-height: 100% !important; }
如果你只是想比较数组,你可以尝试以下方法:
const json_response = {
"data": {
"documents": [{
"fileName": "file01_guid.pdf",
"documentType": "document",
"parentFiles": ["file01.pdf"]
}, {
"fileName": "file02_guid.pdf",
"documentType": "document",
"parentFiles": ["file01.pdf"]
}]
}
};
const allEqual = json_response.data.documents
.every(({ parentFiles: [ filename ] }) => filename === "file01.pdf");
console.log(allEqual);
.as-console-wrapper { top: 0; max-height: 100% !important; }
您可以使用 foreach 循环并比较值
//store expected files in order
array=["file01.pdf","file01.pdf"]
//now forEach on each element
json_response.data.documents.forEach( (elem,index,arr)=>{
pm.expect(elem.parentFiles[0]).to.be.eql(array[index])
})
如果你只是想比较两个数组那么:
pm.expect(_.isEqual(array,array2)).to.be.true
lodash isEqual 可用于比较两个对象,如数组