0
0
Cypresstesting~20 mins

Dynamic response stubbing in Cypress - Practice Problems & Coding Challenges

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

Consider this Cypress test that stubs a GET request dynamically based on the query parameter.

cy.intercept('GET', '/api/items*', (req) => {
  const category = req.query.category;
  if (category === 'books') {
    req.reply({ body: [{ id: 1, name: 'Book A' }] });
  } else {
    req.reply({ body: [] });
  }
}).as('getItems');

cy.visit('/items?category=books');
cy.wait('@getItems').then(({ response }) => {
  console.log(response.body.length);
});
Cypress
cy.intercept('GET', '/api/items*', (req) => {
  const category = req.query.category;
  if (category === 'books') {
    req.reply({ body: [{ id: 1, name: 'Book A' }] });
  } else {
    req.reply({ body: [] });
  }
}).as('getItems');

cy.visit('/items?category=books');
cy.wait('@getItems').then(({ response }) => {
  console.log(response.body.length);
});
A0
B1
Cundefined
DTypeError
Attempts:
2 left
💡 Hint

Think about what the stub returns when the category query is 'books'.

assertion
intermediate
2:00remaining
Which assertion correctly verifies the dynamic stubbed response contains 2 users?

You stub a GET request to '/api/users' that returns 2 user objects dynamically. Which assertion correctly checks the response length?

Cypress
cy.intercept('GET', '/api/users', (req) => {
  req.reply({ body: [{ id: 1 }, { id: 2 }] });
}).as('getUsers');

cy.visit('/users');
cy.wait('@getUsers').its('response.body').should( /* ??? */ );
Ahave.length(2)
Bequal(2)
Ccontain.length(2)
Dlength.equal(2)
Attempts:
2 left
💡 Hint

Check the correct Cypress syntax for asserting array length.

🔧 Debug
advanced
2:00remaining
Why does this dynamic stub not work as expected?

Examine this Cypress intercept code. It should stub different responses based on the request body, but always returns the same response.

cy.intercept('POST', '/api/login', (req) => {
  if (req.body.username === 'admin') {
    req.reply({ statusCode: 200, body: { token: 'admin-token' } });
  }
});
Cypress
cy.intercept('POST', '/api/login', (req) => {
  if (req.body.username === 'admin') {
    req.reply({ statusCode: 200, body: { token: 'admin-token' } });
  }
});
AThe req.reply is called twice causing a conflict.
BThe stub does not handle non-admin usernames, so requests without 'admin' hang.
CThe intercept URL pattern is incorrect and does not match the request.
DThe req.body is undefined because the request is not parsed automatically.
Attempts:
2 left
💡 Hint

Think about how Cypress parses request bodies in intercept handlers.

framework
advanced
2:00remaining
Which Cypress command correctly stubs a dynamic GET response based on URL parameters?

You want to stub GET requests to '/api/products' and return different products based on the 'type' query parameter. Which code snippet achieves this?

A
cy.intercept('GET', '/api/products*', (req) => {
  const type = req.query.type;
  if (type === 'electronics') {
    req.reply({ body: [{ id: 1, name: 'Phone' }] });
  } else {
    req.reply({ body: [] });
  }
});
B
cy.intercept('GET', '/api/products', (req) => {
  if (req.url.includes('type=electronics')) {
    req.reply({ body: [{ id: 1, name: 'Phone' }] });
  } else {
    req.reply({ body: [] });
  }
});
C
cy.intercept('GET', '/api/products?type=electronics', (req) => {
  req.reply({ body: [{ id: 1, name: 'Phone' }] });
});
Dcy.intercept('GET', '/api/products', { body: [{ id: 1, name: 'Phone' }] });
Attempts:
2 left
💡 Hint

Consider how to match any query parameters and access them dynamically.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of dynamic response stubbing in Cypress tests?

Choose the best explanation for why dynamic response stubbing is useful in Cypress testing.

AIt speeds up tests by caching all responses and never making network calls.
BIt automatically generates test data without needing manual input or fixtures.
CIt allows tests to simulate different server responses based on request data, improving test coverage and realism.
DIt replaces the need for writing assertions by validating responses automatically.
Attempts:
2 left
💡 Hint

Think about how dynamic stubbing helps test different scenarios.