How do you mock an async service call in an Angular component test?

This tests Angular unit isolation and HTTP mocking. A strong answer covers HttpTestingController setup, flushing a mock response, asserting UI changes, and notes mocking avoids flaky networks. Red flag: using real HTTP calls in unit tests.
This tests unit test isolation and Angular's HttpClient testing infrastructure. A strong answer describes two approaches: providing a fake service stub in TestBed, or using provideHttpClientTesting with HttpTestingController to intercept the request. They should explain the flush pattern where the test asserts the request was made, delivers a mock payload, and verifies the component renders the result. Mocking is crucial because real network calls make tests slow, non-deterministic, and dependent on external state.
Read the original → angular.dev
- #angular
- #testing
- #httpclient
- #mocking
- #components
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.