Test Overview
This test uses a fixture file to mock the server response for a user list API call. It verifies that the mocked data is displayed correctly on the page.
This test uses a fixture file to mock the server response for a user list API call. It verifies that the mocked data is displayed correctly on the page.
describe('User List with Fixture Mocking', () => { it('displays mocked user data from fixture', () => { cy.intercept('GET', '/api/users', { fixture: 'users.json' }).as('getUsers'); cy.visit('/users'); cy.wait('@getUsers'); cy.get('[data-cy=user-name]').should('have.length', 3); cy.get('[data-cy=user-name]').first().should('contain.text', 'Alice'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Set up intercept to mock GET /api/users with fixture 'users.json' and alias it as 'getUsers' | No page loaded yet, intercept ready to catch API call | - | PASS |
| 2 | Visit the /users page in the browser | Browser loads the /users page, triggering API call to /api/users | - | PASS |
| 3 | Wait for the intercepted API call '@getUsers' to complete | API call intercepted and responded with fixture data | Verify the mocked response was used | PASS |
| 4 | Find all elements with data-cy attribute 'user-name' on the page | Page displays user names from mocked data | Check that there are exactly 3 user name elements | PASS |
| 5 | Check that the first user name element contains text 'Alice' | First user name element visible with text 'Alice' | Verify the first user name matches the fixture data | PASS |