Bird
0
0

How can you test that a method in a service calls a dependency's method using spies in Angular?

hard📝 Application Q9 of 15
Angular - Testing
How can you test that a method in a service calls a dependency's method using spies in Angular?
AUse TestBed.resetTestingModule before each test
BReplace the dependency with a new instance without spying
CCall the service method without injecting dependencies
DInject the dependency, spy on its method, then verify spy was called
Step-by-Step Solution
Solution:
  1. Step 1: Inject dependency to spy on

    Get the dependency instance via TestBed.inject to spy on its methods.
  2. Step 2: Create spy and verify call

    Use spyOn to watch the method, call service method, then check spy call.
  3. Final Answer:

    Inject the dependency, spy on its method, then verify spy was called -> Option D
  4. Quick Check:

    Spy usage for dependency call = D [OK]
Quick Trick: Spy on injected dependency methods to verify calls [OK]
Common Mistakes:
  • Not injecting dependency before spying
  • Replacing dependency without spy
  • Resetting TestBed unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes