0
0
Cypresstesting~10 mins

Asserting response bodies in Cypress - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assert the response status is 200.

Cypress
cy.request('/api/data').then((response) => {
  expect(response.status).to.equal([1])
})
Drag options to blanks, or click blank then click option'
A200
B404
C500
D201
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 or 500 which mean errors.
Using 201 which means resource created, not general success.
2fill in blank
medium

Complete the code to assert the response body has a property 'name'.

Cypress
cy.request('/api/user').then((response) => {
  expect(response.body).to.have.[1]('name')
})
Drag options to blanks, or click blank then click option'
Akeys
Blength
Cinclude
Dproperty
Attempts:
3 left
💡 Hint
Common Mistakes
Using length which checks array length, not object keys.
Using include which checks for substring or array inclusion.
3fill in blank
hard

Fix the error in the assertion to check the response body equals the expected object.

Cypress
const expected = { id: 1, name: 'Alice' };
cy.request('/api/user/1').then((response) => {
  expect(response.body).to.[1](expected)
})
Drag options to blanks, or click blank then click option'
Adeep.equal
Bequals
Ceql
Dequal
Attempts:
3 left
💡 Hint
Common Mistakes
Using equal which compares object references, not content.
Using eql which is an alias but less explicit.
4fill in blank
hard

Fill both blanks to assert the response body array has length 3 and includes an object with id 2.

Cypress
cy.request('/api/items').then((response) => {
  expect(response.body).to.have.length[1];
  expect(response.body).to.deep.include([2]);
})
Drag options to blanks, or click blank then click option'
A(3)
B(2)
C{ id: 2 }
D[{ id: 2 }]
Attempts:
3 left
💡 Hint
Common Mistakes
Using length(2) which is the wrong size.
Using array brackets around the object in deep.include.
5fill in blank
hard

Fill all three blanks to assert the response body has a nested property 'user.email' equal to 'test@example.com'.

Cypress
cy.request('/api/profile').then((response) => {
  expect(response.body).to.have.nested.[1]('[2]').that.equals([3])
})
Drag options to blanks, or click blank then click option'
Aproperty
Buser.email
C'test@example.com'
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using length instead of property.
Not quoting the nested key or expected string properly.