What is the purpose of asserting response bodies in Cypress tests?
Asserting response bodies ensures that the data returned from an API or server matches the expected values, confirming the application behaves correctly.
Click to reveal answer
beginner
How do you access the response body in a Cypress test after intercepting a request?
You use the cy.intercept() command to spy on the request, then in the then() callback, access response.body to assert its contents.
B. The property success does not exist in response.body
C. The then callback should use response.body.success()
D. The assertion should be to.be.true without parentheses
Solution
Step 1: Check the assertion syntax
In Chai assertions used by Cypress, to.be.true is a property, not a function, so it should not have parentheses.
Step 2: Confirm other parts are correct
The URL is provided, and accessing response.body.success is valid if the property exists.
Final Answer:
The assertion should be to.be.true without parentheses -> Option D
Quick Check:
Use to.be.true without () for boolean assertions [OK]
Hint: Boolean assertions use to.be.true without parentheses [OK]
Common Mistakes:
Adding parentheses to boolean assertions
Assuming success is a function
Missing URL in cy.request
5. You want to assert that the response body contains a nested object with a user's id equal to 5 and a roles array containing "admin". Which Cypress assertion correctly checks both conditions?
hard
A. cy.request('/api/user/5').its('body').should((body) => {
expect(body.id).to.eq(5)
expect(body.roles).to.include('admin')
})
B. cy.request('/api/user/5').should('body.id', 5).and('body.roles', 'admin')
C. cy.request('/api/user/5').then((response) => {
expect(response.body.id).to.equal(5)
expect(response.body.roles).to.have.length(1)
})
D. cy.request('/api/user/5').its('body').should('have.property', 'id', 5).and('have.property', 'roles', 'admin')
Solution
Step 1: Understand nested assertions with callback
Using should with a callback allows multiple assertions on the body object.
Step 2: Check each assertion correctness
expect(body.id).to.eq(5) checks the id correctly. expect(body.roles).to.include('admin') checks the roles array contains 'admin'.
Step 3: Identify incorrect options
cy.request('/api/user/5').should('body.id', 5).and('body.roles', 'admin') uses invalid chaining syntax. cy.request('/api/user/5').then((response) => {
expect(response.body.id).to.equal(5)
expect(response.body.roles).to.have.length(1)
}) misses checking 'admin' in roles. cy.request('/api/user/5').its('body').should('have.property', 'id', 5).and('have.property', 'roles', 'admin') wrongly checks roles as a property equal to string 'admin'.
Final Answer:
cy.request('/api/user/5').its('body').should((body) => {
expect(body.id).to.eq(5)
expect(body.roles).to.include('admin')
}) -> Option A
Quick Check:
Use callback in should for multiple nested assertions [OK]
Hint: Use should callback for multiple nested property checks [OK]
Common Mistakes:
Trying to chain multiple properties incorrectly
Checking array length instead of content
Using have.property with array content as string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.