NG.X · Angular 22 practice question

A developer already uses a stub to control what a dependency returns, but now needs to verify that the service under test invoked that dependency with specific arguments. Which test double should they use?

Choose one answer
Check answer and explanation

Correct answer: A spy, which records how a function is called so assertions can verify the arguments.

Explanation

While a stub controls return values, a spy tracks how a function is called so the test can assert on interactions and verify the arguments passed to a dependency.

Common trap

This is tempting because ComponentFixture is central to component testing, but fixtures are for components and DOM elements, not tracking service method calls. This is tempting because stubs replace dependencies in tests, but a plain stub only supplies predictable return values and does not track call arguments.

Source: Angular docs: Testing services — Verifying interactions with spies
Checked against the cited text on 2026-11-25.

Report an error in this question