0
0
Cypresstesting~10 mins

Stubbing responses 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 stub a GET request to '/api/data' with a 200 status.

Cypress
cy.intercept('GET', '/api/data', { statusCode: [1] }).as('getData')
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 simulate errors instead of success.
2fill in blank
medium

Complete the code to stub a POST request to '/api/login' and return a JSON object with a token.

Cypress
cy.intercept('POST', '/api/login', { body: [1] }).as('postLogin')
Drag options to blanks, or click blank then click option'
A{ token: 'abc123' }
B{ error: 'fail' }
C{ status: 401 }
D{ message: 'success' }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning error or status codes instead of a token object.
3fill in blank
hard

Fix the error in the code to stub a GET request to '/api/items' and delay the response by 500ms.

Cypress
cy.intercept('GET', '/api/items', { [1]: 500 }).as('getItems')
Drag options to blanks, or click blank then click option'
Apause
Bdelay
Ctimeout
Dwait
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'timeout', 'wait', or 'pause' which are not valid properties here.
4fill in blank
hard

Fill both blanks to stub a PUT request to '/api/user' returning a 204 status and no body.

Cypress
cy.intercept('PUT', '/api/user', { statusCode: [1], body: [2] }).as('putUser')
Drag options to blanks, or click blank then click option'
A204
Bnull
C{}
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 status with empty object body which implies content.
5fill in blank
hard

Fill all three blanks to stub a DELETE request to '/api/item/123' that returns a 200 status, a JSON message, and delays response by 300ms.

Cypress
cy.intercept('DELETE', '/api/item/123', { statusCode: [1], body: [2], [3]: 300 }).as('deleteItem')
Drag options to blanks, or click blank then click option'
A200
B{ message: 'Deleted successfully' }
Cdelay
DstatusCode
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing property names and values in blanks.
Using wrong status codes or missing delay property.