0
0
Cypresstesting~20 mins

Fixture-based response mocking in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fixture Mocking 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 mocking a GET request with fixture data?
Consider this Cypress test code that mocks a GET request to '/api/user' using a fixture named 'user.json'. What will be the output of the assertion?
Cypress
cy.intercept('GET', '/api/user', { fixture: 'user.json' }).as('getUser');
cy.visit('/profile');
cy.wait('@getUser').its('response.body').should('have.property', 'name', 'Alice');
ATest fails with a network error due to missing server
BTest fails because the fixture file is not loaded automatically
CTest passes because the fixture contains {"name": "Alice"}
DTest passes but the response body is empty
Attempts:
2 left
💡 Hint
Fixtures provide static data to mock API responses in Cypress.
assertion
intermediate
2:00remaining
Which assertion correctly verifies the mocked response status code?
You have mocked a POST request with a fixture in Cypress. Which assertion correctly checks that the response status code is 201?
Cypress
cy.intercept('POST', '/api/create', { fixture: 'createResponse.json' }).as('createItem');
cy.visit('/create');
cy.wait('@createItem').then((interception) => {
  // Assertion here
});
Aexpect(interception.response.statusCode).to.equal(201);
Bexpect(interception.status).to.equal(201);
Cexpect(interception.response.status).to.equal(201);
Dexpect(interception.responseCode).to.equal(201);
Attempts:
2 left
💡 Hint
Check the Cypress interception object properties for response status code.
🔧 Debug
advanced
2:00remaining
Why does the fixture-based mock not work as expected?
This Cypress test tries to mock a GET request with a fixture but the test fails because the real server response is received instead. What is the likely cause?
Cypress
cy.intercept('GET', '/api/data', { fixture: 'data.json' });
cy.visit('/dashboard');
cy.get('.data-item').should('have.length', 5);
AThe intercept URL pattern does not match the actual request URL.
BThe intercept is missing an alias and wait, so it does not block the real request.
CThe cy.visit() call happens before the intercept is set up.
DThe fixture file 'data.json' is empty, so the mock returns no data.
Attempts:
2 left
💡 Hint
Check if the URL pattern in intercept matches the actual request URL exactly.
framework
advanced
2:00remaining
How to dynamically modify fixture data before mocking response?
You want to use fixture data but change the 'id' field dynamically before returning it in the mock response. Which Cypress code snippet achieves this?
A
cy.fixture('item.json').then(data => {
  data.id = 123;
  cy.intercept('GET', '/api/item', data);
});
B
cy.intercept('GET', '/api/item', (req) => {
  cy.fixture('item.json').then(data => {
    data.id = 123;
    req.reply(data);
  });
});
C
cy.intercept('GET', '/api/item', (req) => {
  req.reply({ fixture: 'item.json', id: 123 });
});
Dcy.intercept('GET', '/api/item', { fixture: 'item.json', id: 123 });
Attempts:
2 left
💡 Hint
Use cy.fixture() inside intercept callback to modify data before reply.
🧠 Conceptual
expert
2:00remaining
What is the main advantage of using fixture-based response mocking in Cypress tests?
Choose the best explanation for why fixture-based response mocking is used in Cypress testing.
AIt enables Cypress to record real server responses for later playback without modification.
BIt automatically generates test data based on the application state during runtime.
CIt replaces the need for writing assertions by validating responses automatically.
DIt allows tests to run faster and more reliably by avoiding real network calls and using predefined static data.
Attempts:
2 left
💡 Hint
Think about test speed and reliability when avoiding real servers.