What is a response body array assertion in API testing?
It is a check to confirm that the response from an API contains an array and that this array meets certain conditions like length, content, or structure.
Click to reveal answer
beginner
How do you check if the response body contains an array with at least 3 items in Postman?
Use the assertion: pm.expect(pm.response.json()).to.be.an('array').with.lengthOf.at.least(3);
Click to reveal answer
intermediate
Why is it important to assert the type of response body elements in an array?
To ensure the API returns data in the expected format, which helps avoid errors in later processing or display of data.
Click to reveal answer
intermediate
Show how to assert that every item in a response array has a property 'id' in Postman.
B. Should use pm.expect(item).to.have.property('id') instead
C. forEach cannot be used on jsonData
D. No error, the test is correct
Solution
Step 1: Check if jsonData is an array and forEach usage
pm.response.json() returns an array, so forEach is valid to iterate over items.
Step 2: Validate assertion syntax for property existence
pm.expect(item.id).to.exist correctly asserts that the 'id' property exists on each item.
Final Answer:
No error, the test is correct -> Option D
Quick Check:
forEach and to.exist used correctly [OK]
Hint: Use forEach with to.exist to check property presence [OK]
Common Mistakes:
Thinking forEach can't be used on arrays
Confusing to.exist with to.have.property
Incorrect pm.expect syntax assumptions
5. You want to assert that the response array contains at least one item with status equal to active. Which Postman test code correctly does this?
hard
A. pm.test('Has active status', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.every(item => item.status === 'active')).to.be.true;
});
B. pm.test('Has active status', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.some(item => item.status === 'active')).to.be.true;
});
C. pm.test('Has active status', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.find(item => item.status === 'active')).to.be.undefined;
});
D. pm.test('Has active status', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.filter(item => item.status === 'active').length).to.eql(0);
});
Solution
Step 1: Understand the requirement to find at least one item with status 'active'
The method some() returns true if any item matches the condition, which fits the requirement.
Step 2: Analyze each option's logic
pm.test('Has active status', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.some(item => item.status === 'active')).to.be.true;
}); uses some() and expects true, correctly asserting presence. pm.test('Has active status', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.every(item => item.status === 'active')).to.be.true;
}); uses every(), which requires all items to be active. pm.test('Has active status', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.find(item => item.status === 'active')).to.be.undefined;
}); expects find() result to be undefined, which means no active items. pm.test('Has active status', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.filter(item => item.status === 'active').length).to.eql(0);
}); expects filter length to be 0, meaning no active items.
Final Answer:
pm.test('Has active status', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.some(item => item.status === 'active')).to.be.true;
}); -> Option B
Quick Check:
Use some() to check at least one match [OK]
Hint: Use some() to check if any array item matches condition [OK]
Common Mistakes:
Using every() which requires all items to match
Checking for undefined instead of true
Asserting zero length for presence check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.