Bird
0
0

In this test code, why does the component fail to use the mock service?

medium📝 Debug Q7 of 15
Angular - Services and Dependency Injection
In this test code, why does the component fail to use the mock service?
TestBed.configureTestingModule({
  declarations: [MyComponent],
  providers: [{provide: DataService, useClass: RealDataService}]
});
ABecause useClass cannot be used in providers
BBecause useClass is set to RealDataService instead of the mock
CBecause declarations array is missing DataService
DBecause DataService is not provided at all
Step-by-Step Solution
Solution:
  1. Step 1: Analyze provider configuration

    Provider uses useClass to specify which class to inject for DataService.
  2. Step 2: Identify why mock is not used

    Since useClass points to RealDataService, mock is ignored.
  3. Final Answer:

    Because useClass is set to RealDataService instead of the mock -> Option B
  4. Quick Check:

    Provider useClass must point to mock for testing = D [OK]
Quick Trick: Set useClass to mock service to override real one [OK]
Common Mistakes:
MISTAKES
  • Not providing DataService at all
  • Misplacing services in declarations
  • Thinking useClass cannot be used in providers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes