0
0
Cypresstesting~20 mins

Stubbing responses in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stubbing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the test result when stubbing a GET request with Cypress?

Consider this Cypress test code that stubs a GET request to '/api/user' and returns a fixed user object. What will be the value of response.body.name inside the test?

Cypress
cy.intercept('GET', '/api/user', { statusCode: 200, body: { name: 'Alice', age: 30 } }).as('getUser')
cy.visit('/profile')
cy.wait('@getUser').then(({ response }) => {
  const userName = response.body.name
  cy.wrap(userName).as('userName')
})
cy.get('@userName').should('equal', 'Alice')
Aundefined
B"Bob"
C"Alice"
Dnull
Attempts:
2 left
💡 Hint

Look at the stubbed response body in the cy.intercept call.

assertion
intermediate
2:00remaining
Which assertion correctly verifies the stubbed response status code?

You stub a POST request to '/api/login' with status code 201. Which assertion correctly checks this status code in Cypress?

Cypress
cy.intercept('POST', '/api/login', { statusCode: 201, body: { token: 'abc123' } }).as('login')
cy.visit('/login')
cy.get('button').click()
cy.wait('@login').then(({ response }) => {
  // Which assertion goes here?
})
Aexpect(response.status).to.equal(201)
Bexpect(response.status_code).to.equal(201)
Cexpect(response.code).to.equal(201)
Dexpect(response.statusCode).to.equal(201)
Attempts:
2 left
💡 Hint

Check the exact property name for status code in the response object.

🔧 Debug
advanced
2:00remaining
Why does this Cypress stub not intercept the request?

Look at this Cypress code snippet. The test expects to stub a GET request to '/api/data', but the stub never triggers. What is the likely cause?

Cypress
cy.intercept('GET', '/api/data', { fixture: 'data.json' }).as('getData')
cy.visit('/dashboard')
cy.wait('@getData')
AThe actual request URL includes query parameters, so the stub URL does not match exactly.
BThe fixture file 'data.json' is missing from the fixtures folder.
CThe alias '@getData' is not used in cy.wait correctly.
Dcy.visit does not trigger any network requests.
Attempts:
2 left
💡 Hint

Check if the request URL matches exactly the stub URL.

🧠 Conceptual
advanced
2:00remaining
What is the main benefit of stubbing responses in end-to-end tests?

Why do testers stub network responses in end-to-end tests?

ATo test the backend API logic directly without frontend involvement.
BTo isolate the frontend from backend changes and make tests faster and more reliable.
CTo avoid writing any assertions in tests.
DTo disable all network requests during tests.
Attempts:
2 left
💡 Hint

Think about how stubbing affects test speed and reliability.

framework
expert
2:00remaining
Which Cypress command correctly stubs a delayed response to simulate slow network?

You want to stub a GET request to '/api/items' and delay the response by 2 seconds to simulate slow network. Which command achieves this?

Acy.intercept('GET', '/api/items', { delayMs: 2000, body: [{ id: 1 }] }).as('getItems')
Bcy.intercept('GET', '/api/items', { delay: 2000, body: [{ id: 1 }] }).as('getItems')
Ccy.intercept('GET', '/api/items', { wait: 2000, body: [{ id: 1 }] }).as('getItems')
Dcy.intercept('GET', '/api/items', { timeout: 2000, body: [{ id: 1 }] }).as('getItems')
Attempts:
2 left
💡 Hint

Check the exact property name Cypress uses to delay stubbed responses.