如何验证数组中属性的长度并确保不显示额外的属性?
How to verify the length of attributes in an array and make sure no extra attributes are displayed?
我已将输入的长度验证为 var resp =(body.input).length 并确保它不为 0。但我还想验证每组中只有名字、姓氏和 rollno 可用并且计数将始终为 3。如何验证这是邮递员测试?
响应正文:
{
"success": true,
"input":[
{
firstname:"Ram"
lastname:"Lakshmanan"
rollno: "11"
},
{
firstname:"Pravi"
lastname:"Reshma"
rollno: "12"
}
]
}
我的测试如下所示:
var i;
for(i=0 ; i< resp ; i++){
var resp_firstname = body.input[i].firstname;
pm.test("Verify first name is available and not empty",function(){
pm.expect(resp_firstname).to.exist;
pm.expect(resp_firstname).to.not.eql();
}
首先问题中提供的响应正文不是有效的JSON。
有效 json 数据应如下所示 =
{
"success":true,
"input":[
{
"firstname":"Ram",
"lastname":"Lakshmanan",
"rollno":"11"
},
{
"firstname":"Pravi",
"lastname":"Reshma",
"rollno":"12"
}
]
}
你可以这样做-
pm.test("Verify object has expected properties", function () {
// loop through input array
for (var i = 0; i < Object.keys(json.input).length; i++) {
console.log(i);
pm.expect(json.input[i]).to.have.property("firstname");
pm.expect(json.input[i]).to.have.property("lastname");
pm.expect(json.input[1]).to.have.property("rollno");
//assert length of properties
pm.expect(Object.keys(json.input[i]).length).to.eql(3);
}
});
我已将输入的长度验证为 var resp =(body.input).length 并确保它不为 0。但我还想验证每组中只有名字、姓氏和 rollno 可用并且计数将始终为 3。如何验证这是邮递员测试?
响应正文:
{
"success": true,
"input":[
{
firstname:"Ram"
lastname:"Lakshmanan"
rollno: "11"
},
{
firstname:"Pravi"
lastname:"Reshma"
rollno: "12"
}
]
}
我的测试如下所示:
var i;
for(i=0 ; i< resp ; i++){
var resp_firstname = body.input[i].firstname;
pm.test("Verify first name is available and not empty",function(){
pm.expect(resp_firstname).to.exist;
pm.expect(resp_firstname).to.not.eql();
}
首先问题中提供的响应正文不是有效的JSON。
有效 json 数据应如下所示 =
{
"success":true,
"input":[
{
"firstname":"Ram",
"lastname":"Lakshmanan",
"rollno":"11"
},
{
"firstname":"Pravi",
"lastname":"Reshma",
"rollno":"12"
}
]
}
你可以这样做-
pm.test("Verify object has expected properties", function () {
// loop through input array
for (var i = 0; i < Object.keys(json.input).length; i++) {
console.log(i);
pm.expect(json.input[i]).to.have.property("firstname");
pm.expect(json.input[i]).to.have.property("lastname");
pm.expect(json.input[1]).to.have.property("rollno");
//assert length of properties
pm.expect(Object.keys(json.input[i]).length).to.eql(3);
}
});