liveBook · Manning

Mock data

As explained in Modern Angular

Mock data is deliberately fabricated data supplied by a test in place of data from a real external or application source.

Mock data is deliberately fabricated data supplied by a unit test instead of data from a real application or external source. It makes component behavior predictable while avoiding an actual HTTP call. section 8.3.3

Use mock data to:

  • Represent realistic domain records, such as projects or time-off requests, with only the fields needed by the test. section 8.4.1 section 8.3.3
  • Return predictable values from a mocked service, commonly wrapped in an Observable, so the component can consume them as though an HTTP request occurred. section 8.3.3
  • Exercise additional scenarios by expanding the dataset or changing the mock implementation, including filtering and error cases. section 8.4.1 section 8.3.3

Mocking project data

Define two Project records, then provide them through a mocked ProjectService. A getProject method finds the requested record and returns it with of(...). Render the component with projectId 1 to assert “Project 1”; change the input to 2, detect changes, and assert “Project 2.” section 8.3.3

const timeOffRequests: TimeOffRequest[] = [
    {
        id: 1,
        employeeId: 101,
        startDate: '2023-11-01',
        endDate: '2023-11-03',
        type: 'Vacation',
        status: 'Approved',
        comment: 'Going on a short vacation'
    },
    {
        id: 2,
        employeeId: 102,
        startDate: '2023-11-05',
        endDate: '2023-11-07',
        type: 'Sick Leave',
        status: 'Pending'
    },
];

Inspect a concrete TimeOffRequest array containing dates, leave types, statuses, and an optional comment. section 8.4.1

How Modern Angular covers it

How deep it goes
Demonstrates defining records, returning them from mocked service methods, updating inputs, and extending scenarios, but defers exhaustive case design.
Examples
Uses Project records and TimeOffRequest records, including filtering by leave type and rendering changed project names.
What it assumes
Assumes Angular unit-testing and TypeScript knowledge while teaching how to substitute service and HTTP results with typed records.
What you can do after
Apply it via Angular Testing Library and mocked services to verify predictable component rendering and input-driven updates.
How it connects
Connects mock data with unit testing, mocked services, Angular Testing Library, HTTP calls, Jest, and Observables.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern Angular. The section links point to the parts of the book it draws on.