Fixture-based response mocking helps you test your app by pretending the server sends fixed data. This way, you can check how your app works without needing a real server.
Fixture-based response mocking in Cypress
cy.intercept('GET', '/api/data', { fixture: 'data.json' })
cy.intercept is used to catch network requests and replace their response.
The fixture option tells Cypress to use a file from the cypress/fixtures folder as the response.
cy.intercept('GET', '/users', { fixture: 'users.json' })
cy.intercept('POST', '/login', { fixture: 'login-success.json' })
cy.intercept('GET', '/products', { fixture: 'products.json' }).as('getProducts')
This test mocks the GET request to '/api/users' with data from 'users.json'. It visits the users page, waits for the mocked request, and checks if the page shows the user name 'Alice' from the fixture.
describe('Fixture-based response mocking example', () => { it('shows mocked user data', () => { cy.intercept('GET', '/api/users', { fixture: 'users.json' }).as('getUsers') cy.visit('/users-page') cy.wait('@getUsers') cy.get('.user-name').should('contain.text', 'Alice') }) })
Put your fixture files inside the cypress/fixtures folder.
Use cy.wait('@alias') to wait for the mocked request to finish before checking the page.
Fixture mocking works well for stable, repeatable tests without depending on real servers.
Fixture-based mocking replaces real server responses with fixed data files.
This helps test your app in a fast, reliable, and controlled way.
Use cy.intercept with the fixture option to mock responses easily.
Practice
Solution
Step 1: Understand fixture-based mocking concept
Fixture-based mocking replaces actual server responses with predefined data files to control test conditions.Step 2: Identify the main benefit
This approach ensures tests run fast and reliably by avoiding real server calls and unpredictable data.Final Answer:
To replace real server responses with fixed data files for consistent testing -> Option AQuick Check:
Fixture mocking = fixed data for reliable tests [OK]
- Confusing fixture mocking with UI rendering speed
- Thinking fixture mocking generates data automatically
- Assuming fixture mocking records user actions
Solution
Step 1: Recall Cypress intercept syntax for fixture mocking
The correct way is to use cy.intercept with method, URL, and an object with fixture property.Step 2: Match the correct option
cy.intercept('GET', '/api/users', { fixture: 'users.json' }) uses cy.intercept with { fixture: 'users.json' }, which is the correct syntax.Final Answer:
cy.intercept('GET', '/api/users', { fixture: 'users.json' }) -> Option CQuick Check:
Use cy.intercept + fixture option [OK]
- Using cy.route instead of cy.intercept (deprecated)
- Passing fixture file as body string
- Using cy.mock which doesn't exist
cy.intercept('GET', '/api/products', { fixture: 'products.json' })
cy.visit('/products')
Assume 'products.json' contains [{"id":1,"name":"Book"}].Solution
Step 1: Understand intercept with fixture
The intercept replaces the GET /api/products response with the content of 'products.json'.Step 2: Check fixture content
The fixture file contains [{"id":1,"name":"Book"}], so this will be the mocked response body.Final Answer:
[{"id":1,"name":"Book"}] -> Option AQuick Check:
Fixture content = mocked response body [OK]
- Expecting real server data instead of fixture
- Assuming empty array if fixture not found
- Thinking intercept causes network error
cy.intercept('POST', '/api/users', { fixture: 'newUser.json' })
cy.visit('/users')Solution
Step 1: Review intercept syntax for POST with fixture
For POST requests, the fixture must be inside a response object like { body: { fixture: 'file' } } or use { fixture: 'file' } directly as response.Step 2: Identify the mistake
Passing { fixture: 'newUser.json' } directly as the third argument is correct syntax in Cypress 12+, so no error there. But if the Cypress version is older, it requires response object. However, the main common error is misunderstanding the fixture placement.Step 3: Check other options
POST requests can be mocked with fixtures, file extension is allowed, and cy.intercept should come before cy.visit.Final Answer:
The fixture option should be inside a response object, not directly in intercept -> Option DQuick Check:
Fixture must be correctly placed in intercept options [OK]
- Thinking POST cannot be mocked with fixtures
- Ignoring correct order of cy.intercept and cy.visit
- Removing .json extension from fixture filename
Solution
Step 1: Understand mocking multiple endpoints
Each endpoint requires its own intercept call to specify the URL and fixture separately.Step 2: Evaluate options
Call cy.intercept twice, once for each endpoint with its fixture correctly calls cy.intercept twice, once per endpoint, which is the best practice.Step 3: Reject other options
Combining fixtures or URLs is not supported; mocking only one endpoint leaves tests unreliable.Final Answer:
Call cy.intercept twice, once for each endpoint with its fixture -> Option BQuick Check:
One intercept per endpoint with fixture [OK]
- Trying to mock multiple URLs in one intercept call
- Combining fixtures into one file unnecessarily
- Mocking only some endpoints and ignoring others
Ready to prove it?
Pass the test and this pattern moves unknown
