Choosing between HTTP-layer mocking and service stubbing in Angular integration tests
0 reputation · 30 Sept 2024, 07:48 UTC
HTTP-layer vs service-layer mocking in Angular
When testing services that make HTTP calls, Angular provides HttpClientTestingModule with HttpTestingController to intercept requests at the HTTP layer. This approach mocks the backend responses without requiring production credentials or network access.
Alternatively, developers can stub the service itself by creating a mock implementation that returns predefined observables. This tests different layers of the application stack.
The unresolved decision centers on what integration behavior each approach actually covers. HTTP-layer mocking validates how the service processes responses and handles errors, but does not verify the service's internal logic or contract adherence. Service stubbing may miss HTTP-specific behaviors like interceptor effects, request cancellation, or timing issues.
Which integration concerns should be validated in unit tests versus integration tests, and how does the choice between HttpTestingController and service stubbing affect test coverage and maintenance burden?