Test Overview
This test stubs a network response for a user API call and verifies the UI shows the stubbed user name.
This test stubs a network response for a user API call and verifies the UI shows the stubbed user name.
describe('User API stub test', () => { it('stubs the /api/user response and checks UI', () => { cy.intercept('GET', '/api/user', { fixture: 'user.json' }).as('getUser'); cy.visit('/profile'); cy.wait('@getUser'); cy.get('[data-cy=user-name]').should('contain.text', 'John Doe'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | cy.intercept stubs GET /api/user with fixture user.json and aliases it as getUser | No browser interaction yet, stub is ready | - | PASS |
| 2 | cy.visit navigates to /profile page | Browser loads /profile page, triggers GET /api/user request | - | PASS |
| 3 | cy.wait waits for the aliased getUser network call to complete | Stubbed response from user.json is returned instead of real API | Verify network call intercepted and stubbed response received | PASS |
| 4 | cy.get finds element with data-cy=user-name and checks its text | Page shows user name from stubbed response | Assert element text contains 'John Doe' | PASS |