What is fixture-based response mocking in Cypress?
It is a way to simulate server responses by using predefined data files called fixtures. This helps test how the app behaves with specific data without needing a real server.
Click to reveal answer
beginner
How do you load a fixture file in a Cypress test?
Use cy.fixture('filename').then((data) => { ... }) to load the fixture data and use it in your test.
Click to reveal answer
beginner
Why use fixture-based mocking instead of real API calls?
It makes tests faster, more reliable, and independent from the server. You control the data, so tests don’t fail because of network or server issues.
Click to reveal answer
intermediate
Which Cypress command is used to intercept a network request and respond with fixture data?
Use cy.intercept('GET', '/api/endpoint', { fixture: 'data.json' }) to mock the response with fixture data.
Click to reveal answer
beginner
What is the folder name where Cypress fixture files are stored by default?
The default folder is cypress/fixtures. Place your JSON or other data files there for easy access.
Click to reveal answer
Which command loads fixture data in Cypress?
Acy.fixture('file.json')
Bcy.load('file.json')
Ccy.mock('file.json')
Dcy.data('file.json')
✗ Incorrect
The correct command to load fixture data is cy.fixture('file.json').
Where should fixture files be placed in a Cypress project?
Acypress/plugins
Bcypress/integration
Ccypress/support
Dcypress/fixtures
✗ Incorrect
Fixture files go inside the cypress/fixtures folder by default.
What does cy.intercept() do when used with a fixture option?
AIntercepts a request and responds with fixture data
BLoads fixture data into the test context
CSaves response data to a fixture file
DRuns the test with fixture data
✗ Incorrect
cy.intercept() can mock network requests by responding with fixture data.
Why is fixture-based mocking useful in tests?
AIt slows down tests
BIt requires a live server
CIt makes tests faster and more stable
DIt hides test failures
✗ Incorrect
Fixture mocking avoids real network calls, making tests faster and reliable.
How do you access fixture data inside a .then() callback?
Acy.load('file').then((data) => { use data })
Bcy.fixture('file').then((data) => { use data })
Ccy.mock('file').then((data) => { use data })
Dcy.data('file').then((data) => { use data })
✗ Incorrect
Use cy.fixture('file').then((data) => { ... }) to access fixture data.
Explain how to use fixture-based response mocking in Cypress to test an API call.
Think about intercepting requests and using saved data files.
You got /4 concepts.
Describe the benefits of using fixture-based mocking in your tests.
Consider what problems mocking solves.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using fixture-based response mocking in Cypress tests?
easy
A. To replace real server responses with fixed data files for consistent testing
B. To speed up the browser rendering process
C. To automatically generate test data from the server
D. To record user interactions for playback
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 A
Quick Check:
Fixture mocking = fixed data for reliable tests [OK]
Hint: Fixture mocking means fixed data replaces server calls [OK]
Common Mistakes:
Confusing fixture mocking with UI rendering speed
Thinking fixture mocking generates data automatically
Assuming fixture mocking records user actions
2. Which of the following is the correct syntax to mock a GET request to '/api/users' using a fixture named 'users.json' in Cypress?
easy
A. cy.mock('GET', '/api/users', 'users.json')
B. cy.intercept('GET', '/api/users', { body: 'users.json' })
C. cy.intercept('GET', '/api/users', { fixture: 'users.json' })
D. cy.route('GET', '/api/users', 'fixture:users.json')
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 C
Quick Check:
Use cy.intercept + fixture option [OK]
Hint: Use cy.intercept with { fixture: 'file.json' } [OK]
Common Mistakes:
Using cy.route instead of cy.intercept (deprecated)
Passing fixture file as body string
Using cy.mock which doesn't exist
3. Given the following Cypress code snippet, what will be the response body when the app requests '/api/products'?
B. The fixture file name must not include the .json extension
C. cy.visit should come before cy.intercept
D. The fixture option should be inside a response object, not directly in intercept
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 D
Quick Check:
Fixture must be correctly placed in intercept options [OK]
Hint: Place fixture inside response object for POST mocks [OK]
Common Mistakes:
Thinking POST cannot be mocked with fixtures
Ignoring correct order of cy.intercept and cy.visit
Removing .json extension from fixture filename
5. You want to mock multiple API endpoints in a single Cypress test using fixtures: '/api/users' with 'users.json' and '/api/orders' with 'orders.json'. Which is the best way to do this?
hard
A. Mock only one endpoint and let the other call the real server
B. Call cy.intercept twice, once for each endpoint with its fixture
C. Use cy.intercept with an array of URLs and a single fixture
D. Combine both fixtures into one file and mock a single endpoint
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 B
Quick Check:
One intercept per endpoint with fixture [OK]
Hint: Use separate cy.intercept calls per endpoint [OK]
Common Mistakes:
Trying to mock multiple URLs in one intercept call
Combining fixtures into one file unnecessarily
Mocking only some endpoints and ignoring others
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.