如何检查数组是否在 Postman 中有对象
How can I check if array has an objects in Postman
我对数组的响应是这样的:
[
{
"id": 1,
"name": "project one"
},
{
"id": 2,
"name": "project two"
},
{
"id": 3,
"name": "project three"
}
]
我可以检查我的响应数组是否有一个对象{
“编号”:3,
"name": "项目三"
} 例如?
我正在尝试通过这种方式进行检查,但没有成功:
pm.test('The array have object', () => {
pm.expect(jsonData).to.include(myObject)
})
pm.expect(jsonData).to.include(myObject)
适用于字符串但不适用于对象。您应该使用以下函数之一并比较对象的每个 属性:
- Array.filter()
- Array.find()
- Array.some()
示例:
data = [
{
"id": 1,
"name": "project one"
},
{
"id": 2,
"name": "project two"
},
{
"id": 3,
"name": "project three"
}
];
let object_to_find = { id: 3, name: 'project three' }
// Returns the first match
let result1 = data.find(function (value) {
return value.id == object_to_find.id && value.name == object_to_find.name;
});
// Get filtered array
let result2 = data.filter(function (value) {
return value.id == object_to_find.id && value.name == object_to_find.name;
});
// Returns true if some values pass the test
let result3 = data.some(function (value) {
return value.id == object_to_find.id && value.name == object_to_find.name;
});
console.log("result1: " + result1.id + ", " + result1.name);
console.log("result2 size: " + result2.length);
console.log("result3: " + result3);
在 Postman 中断言时使用其中一种方法。
在使用 JSON.stringify
将其转换为字符串后,您还可以使用 include 验证这一点
pm.expect(JSON.stringify(data)).to.include(JSON.stringify({
"id": 3,
"name": "project three"
}))
你也可以使用 lodash 函数 some/any:
pm.expect(_.some(data,{
"id": 3,
"name": "project three"
})).to.be.true
https://lodash.com/docs/3.10.1#some
注意: Postman 在沙箱中工作,仅支持以下库:
我对数组的响应是这样的:
[
{
"id": 1,
"name": "project one"
},
{
"id": 2,
"name": "project two"
},
{
"id": 3,
"name": "project three"
}
]
我可以检查我的响应数组是否有一个对象{ “编号”:3, "name": "项目三" } 例如? 我正在尝试通过这种方式进行检查,但没有成功:
pm.test('The array have object', () => {
pm.expect(jsonData).to.include(myObject)
})
pm.expect(jsonData).to.include(myObject)
适用于字符串但不适用于对象。您应该使用以下函数之一并比较对象的每个 属性:
- Array.filter()
- Array.find()
- Array.some()
示例:
data = [
{
"id": 1,
"name": "project one"
},
{
"id": 2,
"name": "project two"
},
{
"id": 3,
"name": "project three"
}
];
let object_to_find = { id: 3, name: 'project three' }
// Returns the first match
let result1 = data.find(function (value) {
return value.id == object_to_find.id && value.name == object_to_find.name;
});
// Get filtered array
let result2 = data.filter(function (value) {
return value.id == object_to_find.id && value.name == object_to_find.name;
});
// Returns true if some values pass the test
let result3 = data.some(function (value) {
return value.id == object_to_find.id && value.name == object_to_find.name;
});
console.log("result1: " + result1.id + ", " + result1.name);
console.log("result2 size: " + result2.length);
console.log("result3: " + result3);
在 Postman 中断言时使用其中一种方法。
在使用 JSON.stringify
将其转换为字符串后,您还可以使用 include 验证这一点pm.expect(JSON.stringify(data)).to.include(JSON.stringify({
"id": 3,
"name": "project three"
}))
你也可以使用 lodash 函数 some/any:
pm.expect(_.some(data,{
"id": 3,
"name": "project three"
})).to.be.true
https://lodash.com/docs/3.10.1#some
注意: Postman 在沙箱中工作,仅支持以下库: