Angular - TestingYou want to mock a service method that returns different values on consecutive calls in Angular tests. Which approach correctly achieves this?ACreate a mock class with a method using a call count variable to return different valuesBUse useValue with a plain object having the method returning a fixed valueCUse useClass with the real service and override the method in the testDInject the real service and spy on the method without mockingCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand requirement for different returnsReturning different values on consecutive calls requires state tracking inside the mock method.Step 2: Choose correct mocking approachA mock class with a call count variable can track calls and return different values accordingly.Step 3: Evaluate other optionsuseValue with fixed return can't vary returns; overriding real service method is complex; spying alone doesn't mock service.Final Answer:Create a mock class with a method using a call count variable to return different values -> Option AQuick Check:Mock class with state tracks calls for varied returns [OK]Quick Trick: Use mock class with call count to vary method returns [OK]Common Mistakes:Using fixed return object for varying outputsOverriding real service instead of mockingRelying only on spies without mocks
Master "Testing" in Angular9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More Angular Quizzes Angular Signals - Migrating from observables to signals - Quiz 8hard Animations - Trigger and state definitions - Quiz 9hard Animations - Why Angular animations matter - Quiz 7medium Internationalization and Accessibility - ARIA attributes in templates - Quiz 14medium Internationalization and Accessibility - Why i18n matters - Quiz 14medium Server-Side Rendering - TransferState for data sharing - Quiz 4medium Standalone Components - Standalone pipes and directives - Quiz 12easy Standalone Components - Why standalone components matter - Quiz 13medium State Management - NgRx store concept - Quiz 11easy State Management - Service-based state management - Quiz 10hard