0
0
Cypresstesting~10 mins

Dynamic response stubbing 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 with a dynamic response.

Cypress
cy.intercept('GET', '/api/users', [1]).as('getUsers')
Drag options to blanks, or click blank then click option'
A{ fixture: 'users.json' }
B{ statusCode: 200, body: [{ id: 1, name: 'Alice' }] }
C{ delay: 1000 }
D{ method: 'POST' }
Attempts:
3 left
💡 Hint
Common Mistakes
Using fixture instead of inline response.
Setting wrong HTTP method in stub.
Omitting statusCode in response.
2fill in blank
medium

Complete the code to stub a POST request and return a custom response.

Cypress
cy.intercept('POST', '/api/login', (req) => { req.reply({ [1] }) }).as('postLogin')
Drag options to blanks, or click blank then click option'
AstatusCode: 200, body: { token: 'abc123' }
BstatusCode: 404, body: { error: 'Not found' }
CstatusCode: 500, body: { error: 'Server error' }
DstatusCode: 401, body: { error: 'Unauthorized' }
Attempts:
3 left
💡 Hint
Common Mistakes
Using error status codes for a successful login stub.
Omitting the token in the response body.
3fill in blank
hard

Fix the error in the stub to correctly delay the response by 2 seconds.

Cypress
cy.intercept('GET', '/api/data', { [1] }).as('getData')
Drag options to blanks, or click blank then click option'
Atimeout: 2000, body: { data: [] }
Bwait: 2000, body: { data: [] }
Cpause: 2000, body: { data: [] }
Ddelay: 2000, body: { data: [] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect property names like wait, timeout, or pause.
Omitting the delay property.
4fill in blank
hard

Fill both blanks to stub a GET request that returns a 404 status with a custom error message.

Cypress
cy.intercept('GET', '/api/item/[1]', { statusCode: [2], body: { error: 'Item not found' } }).as('getItem')
Drag options to blanks, or click blank then click option'
A:id
B404
C500
D:itemId
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong dynamic segment syntax like ':itemId'.
Using wrong status code like 500 instead of 404.
5fill in blank
hard

Fill all three blanks to stub a PUT request that updates a user and returns the updated user data with status 200.

Cypress
cy.intercept('PUT', '/api/users/[1]', (req) => { req.reply({ statusCode: [2], body: [3] }) }).as('updateUser')
Drag options to blanks, or click blank then click option'
A:userId
B200
C{ id: req.body.id, name: req.body.name }
D201
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong status code like 201 for update.
Not returning updated user data in the body.
Using incorrect dynamic segment syntax.