Test Overview
This test uses Cypress to stub a dynamic API response based on the request query parameter. It verifies that the page displays the correct user name from the stubbed response.
This test uses Cypress to stub a dynamic API response based on the request query parameter. It verifies that the page displays the correct user name from the stubbed response.
describe('Dynamic response stubbing test', () => { it('stubs API response dynamically based on userId', () => { cy.intercept('GET', '/api/user*', (req) => { const userId = req.query.userId; if (userId === '1') { req.reply({ statusCode: 200, body: { id: 1, name: 'Alice' } }); } else if (userId === '2') { req.reply({ statusCode: 200, body: { id: 2, name: 'Bob' } }); } else { req.reply({ statusCode: 404, body: { error: 'User not found' } }); } }).as('getUser'); cy.visit('/user-profile?userId=1'); cy.wait('@getUser'); cy.get('[data-cy=user-name]').should('contain.text', 'Alice'); cy.visit('/user-profile?userId=2'); cy.wait('@getUser'); cy.get('[data-cy=user-name]').should('contain.text', 'Bob'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and sets up intercept for GET /api/user with dynamic response based on userId query | Cypress test runner ready with intercept active | — | PASS |
| 2 | Browser opens and navigates to /user-profile?userId=1 | Page loading with userId=1 in URL | — | PASS |
| 3 | Intercept catches GET /api/user?userId=1 and replies with {id:1, name:'Alice'} | API response stubbed with Alice's data | — | PASS |
| 4 | Test waits for @getUser request to complete | API call completed with stubbed response | — | PASS |
| 5 | Test finds element with data-cy=user-name and checks text contains 'Alice' | User name displayed on page | Assert element text contains 'Alice' | PASS |
| 6 | Browser navigates to /user-profile?userId=2 | Page loading with userId=2 in URL | — | PASS |
| 7 | Intercept catches GET /api/user?userId=2 and replies with {id:2, name:'Bob'} | API response stubbed with Bob's data | — | PASS |
| 8 | Test waits for @getUser request to complete | API call completed with stubbed response | — | PASS |
| 9 | Test finds element with data-cy=user-name and checks text contains 'Bob' | User name displayed on page | Assert element text contains 'Bob' | PASS |
cy.intercept in Cypress?cy.intercept('GET', '/api/user', (req) => {
const id = req.query.id
if (id === '1') {
req.reply({ body: { name: 'Alice' } })
} else {
req.reply({ body: { name: 'Guest' } })
}
})The app calls fetch('/api/user?id=1'). What is the response?
cy.intercept('POST', '/api/login', (req) => {
req.reply({ statusCode: 200, body: { token: 'abc123' } })
})The test fails with an error about req.reply not being a function. What is wrong?
interception, which has the reply method.req as parameter is allowed but req.reply is not a function because the object is not the request but the interception. The correct approach is to name it interception and call interception.reply().interception and interception.reply used -> Option B{ items: ['apple', 'banana'] }. If 'type' is 'vegetable', respond with { items: ['carrot', 'lettuce'] }. Otherwise, respond with { items: [] }. Which code correctly implements this dynamic response stubbing?interception (or similar) and use interception.reply() to send the response with a body property.interception.query.type and calls interception.reply({ body: ... }). cy.intercept('GET', '/api/items', (req) => {
if (req.query.type === 'fruit') {
req.reply({ items: ['apple', 'banana'] })
} else if (req.query.type === 'vegetable') {
req.reply({ items: ['carrot', 'lettuce'] })
} else {
req.reply({ items: [] })
}
}) misses body wrapper. cy.intercept('GET', '/api/items', (req) => {
const type = req.query.type
if (type === 'fruit') {
req.reply({ body: { items: ['apple', 'banana'] } })
} else if (type === 'vegetable') {
req.reply({ body: { items: ['carrot', 'lettuce'] } })
} else {
req.reply({ body: { items: [] } })
}
}) uses req but req.reply is not a function. cy.intercept('GET', '/api/items', (interception) => {
const type = interception.query.type
if (type === 'fruit') {
interception.send({ body: { items: ['apple', 'banana'] } })
} else if (type === 'vegetable') {
interception.send({ body: { items: ['carrot', 'lettuce'] } })
} else {
interception.send({ body: { items: [] } })
}
}) uses interception.send which is not a Cypress method.Pass the test and this pattern moves unknown