Performance: Service testing with dependency injection
MEDIUM IMPACT
This concept affects test execution speed and resource usage during development, indirectly impacting developer productivity and feedback loop time.
const mockService = { fetchData: () => of(mockData) }; TestBed.configureTestingModule({ providers: [{ provide: RealService, useValue: mockService }, DependentService] }); const service = TestBed.inject(DependentService); // Tests use lightweight mocks
TestBed.configureTestingModule({ providers: [RealService, DependentService] });
const service = TestBed.inject(DependentService);
// Tests use real dependencies| Pattern | Test Execution Time | Resource Usage | Reliability | Verdict |
|---|---|---|---|---|
| Using real dependencies | High (slow tests) | High (network, CPU) | Low (flaky due to side effects) | [X] Bad |
| Using dependency injection with mocks | Low (fast tests) | Low (minimal CPU/network) | High (isolated, reliable) | [OK] Good |