邮递员 - 我想检查一个值是否在数组内
Postman - I want to check a value to be inside an array
我正在 JavaScript 中编写邮递员测试来断言下面的场景。
我有一个 id 1111
和响应 returns 一个 id 数组。我想编写一个测试来将 1111 匹配到数组中的一个 ID。
我尝试使用包含函数,即
pm.test("The response contains a valid id in the response", function() {
pm.expect(jsonData.results[0].totalId.children).to.include("1111");
});
{
"totalId": "0000",
"children": [{
"id": "888"
},
{
"id": "3323"
},
{
"id": "1111"
}
]
}
任何建议。
正如@Justinas 提到的,您必须检查您的 ID,它是“111”,并且您正在检查“1111”。此外,您还必须更改 JSON 的结构,您在数组中缺少“[” .
{"totalId":"0000",
"children": [
{
"id": "888"
},
{
"id": "3323"
},
{
"id": "111"
}
]
}
最重要的是您的测试代码应该更改为:
pm.test("The response contains a valid id in the response", function () { pm.expect(jsonData.results[0].totalId.children[2]).to.eql("111"); });
您正在尝试检查数组 children
中的值,因此,您不应使用 .totalId.children
,而您必须做的是 jsonData.results[0].children
.
当您尝试检查数组对象的值时,您需要添加自定义 JavaScript 逻辑来检查 id
参数的值。
这是您可以在测试脚本中使用的函数。
function _isContains(json, keyname, value) {
return Object.keys(json).some(key => {
return typeof json[key] === 'object' ?
_isContains(json[key], keyname, value) : key === keyname && json[key] === value;
});
}
函数 _isContains
检查数组中每个对象的值和键名称。
现在您可以将需要的输入传递给测试用例中的函数。
pm.test("The response contains a valid id in the response", function () {
pm.expect(_isContains(jsonData.children, "id" ,"1111")).to.be.true;
});
这将从具有键 id
和值 1111
的对象数组中进行检查,如果它可用则它将 returns 为真,否则为 returns 假.
最终测试脚本
var jsonData = pm.response.json();
pm.test("The response contains a valid id in the response", function () {
pm.expect(_isContains(jsonData.children, "id" ,"1111")).to.be.true;
});
function _isContains(json, keyname, value) {
return Object.keys(json).some(key => {
return typeof json[key] === 'object' ?
_isContains(json[key], keyname, value) : key === keyname && json[key] === value;
});
}
如果 JSON 对象包含 null
值,_isContains
函数需要一个小的修改:&& json[key] !== null
:
{
"totalId":"0000",
"children": [
{
"id": "888",
"value": null
},
{
"id": "3323",
"value": null
},
{
"id": "111",
"value": null
}
]
}
function _isContains(json, keyname, value) {
return Object.keys(json).some(key => {
return typeof json[key] === 'object' && json[key] !== null ?
_isContains(json[key], keyname, value) : key === keyname && json[key] === value;
});
}
一个更简单的解决方案可能是:
const child = jsonData.results[0].totalId.children.find(c => c.id === "1111");
pm.expect(child).exist;
此方法的优点是您可以轻松扩展测试套件以验证扩展 keys/values(如 @Ajeet Shah 的示例所述):
pm.expect(child.value).is.not.null;
注意:假设数组中只有一项的 ID 为“1111”。实际上,find() 方法 returns 提供的数组中满足提供的测试函数的第一个元素的值。如果没有满足测试函数的值,则返回undefined。
你的代码有错误,children是totalId的兄弟,不是child。
我会这样做:
pm.test("The response contains a valid id in the response", function () {
var jsonData = pm.response.json();
var ids = jsonData.children.map((i) => i.id);
pm.expect(ids.includes('1111')).to.eql(true);
});
我正在 JavaScript 中编写邮递员测试来断言下面的场景。
我有一个 id 1111
和响应 returns 一个 id 数组。我想编写一个测试来将 1111 匹配到数组中的一个 ID。
我尝试使用包含函数,即
pm.test("The response contains a valid id in the response", function() {
pm.expect(jsonData.results[0].totalId.children).to.include("1111");
});
{
"totalId": "0000",
"children": [{
"id": "888"
},
{
"id": "3323"
},
{
"id": "1111"
}
]
}
任何建议。
正如@Justinas 提到的,您必须检查您的 ID,它是“111”,并且您正在检查“1111”。此外,您还必须更改 JSON 的结构,您在数组中缺少“[” .
{"totalId":"0000",
"children": [
{
"id": "888"
},
{
"id": "3323"
},
{
"id": "111"
}
]
}
最重要的是您的测试代码应该更改为:
pm.test("The response contains a valid id in the response", function () { pm.expect(jsonData.results[0].totalId.children[2]).to.eql("111"); });
您正在尝试检查数组 children
中的值,因此,您不应使用 .totalId.children
,而您必须做的是 jsonData.results[0].children
.
当您尝试检查数组对象的值时,您需要添加自定义 JavaScript 逻辑来检查 id
参数的值。
这是您可以在测试脚本中使用的函数。
function _isContains(json, keyname, value) {
return Object.keys(json).some(key => {
return typeof json[key] === 'object' ?
_isContains(json[key], keyname, value) : key === keyname && json[key] === value;
});
}
函数 _isContains
检查数组中每个对象的值和键名称。
现在您可以将需要的输入传递给测试用例中的函数。
pm.test("The response contains a valid id in the response", function () {
pm.expect(_isContains(jsonData.children, "id" ,"1111")).to.be.true;
});
这将从具有键 id
和值 1111
的对象数组中进行检查,如果它可用则它将 returns 为真,否则为 returns 假.
最终测试脚本
var jsonData = pm.response.json();
pm.test("The response contains a valid id in the response", function () {
pm.expect(_isContains(jsonData.children, "id" ,"1111")).to.be.true;
});
function _isContains(json, keyname, value) {
return Object.keys(json).some(key => {
return typeof json[key] === 'object' ?
_isContains(json[key], keyname, value) : key === keyname && json[key] === value;
});
}
如果 JSON 对象包含 null
值,_isContains
函数需要一个小的修改:&& json[key] !== null
:
{
"totalId":"0000",
"children": [
{
"id": "888",
"value": null
},
{
"id": "3323",
"value": null
},
{
"id": "111",
"value": null
}
]
}
function _isContains(json, keyname, value) {
return Object.keys(json).some(key => {
return typeof json[key] === 'object' && json[key] !== null ?
_isContains(json[key], keyname, value) : key === keyname && json[key] === value;
});
}
一个更简单的解决方案可能是:
const child = jsonData.results[0].totalId.children.find(c => c.id === "1111");
pm.expect(child).exist;
此方法的优点是您可以轻松扩展测试套件以验证扩展 keys/values(如 @Ajeet Shah 的示例所述):
pm.expect(child.value).is.not.null;
注意:假设数组中只有一项的 ID 为“1111”。实际上,find() 方法 returns 提供的数组中满足提供的测试函数的第一个元素的值。如果没有满足测试函数的值,则返回undefined。
你的代码有错误,children是totalId的兄弟,不是child。
我会这样做:
pm.test("The response contains a valid id in the response", function () {
var jsonData = pm.response.json();
var ids = jsonData.children.map((i) => i.id);
pm.expect(ids.includes('1111')).to.eql(true);
});