Angular - TestingHow can you test that a method in a service calls a dependency's method using spies in Angular?AUse TestBed.resetTestingModule before each testBReplace the dependency with a new instance without spyingCCall the service method without injecting dependenciesDInject the dependency, spy on its method, then verify spy was calledCheck Answer
Step-by-Step SolutionSolution:Step 1: Inject dependency to spy onGet the dependency instance via TestBed.inject to spy on its methods.Step 2: Create spy and verify callUse spyOn to watch the method, call service method, then check spy call.Final Answer:Inject the dependency, spy on its method, then verify spy was called -> Option DQuick 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 spyingReplacing dependency without spyResetting TestBed unnecessarily
Master "Testing" in Angular9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More Angular Quizzes Advanced Patterns - Smart and dumb component pattern - Quiz 13medium Advanced Patterns - Why design patterns matter - Quiz 2easy Angular Signals - Computed signals for derived values - Quiz 7medium Angular Signals - Why signals are introduced - Quiz 10hard Animations - Keyframe animations - Quiz 1easy Animations - BrowserAnimationsModule setup - Quiz 13medium Performance Optimization - Web workers for heavy computation - Quiz 2easy Performance Optimization - Why performance tuning matters - Quiz 5medium State Management - Why state management matters - Quiz 10hard Testing - Testing with fixtures and debug elements - Quiz 2easy