NG.X · Angular 22 practice question
A developer wants to unit test OrderTotal without depending on the real TaxCalculator implementation. What is the recommended way to isolate OrderTotal?
Check answer and explanation
Correct answer: Provide a stub for TaxCalculator in the TestBed configuration so it returns predictable values.
Explanation
Providing a stub for TaxCalculator in the TestBed configuration replaces the real dependency with one that returns predictable values, making OrderTotal's behavior easy to verify.
Common trap
This is tempting because browser mode changes the test environment, but it does not replace or isolate a service dependency. This is tempting because HttpTestingController isolates HTTP calls, but it only intercepts HttpClient requests, not direct method calls on another service.
Source: Angular docs: Testing services — Replacing a dependency with a stub
Checked against the cited text on 2026-11-25.