Performance: Mocking services in tests
LOW IMPACT
Mocking services in tests affects test execution speed and resource usage during development, not the actual page load or runtime performance.
import { of } from 'rxjs'; class MockService { getData() { return of(['mock', 'data']); } } beforeEach(() => { TestBed.configureTestingModule({ providers: [{ provide: RealService, useClass: MockService }] }); }); it('should test with mock service', () => { const service = TestBed.inject(RealService); // fast, no real HTTP calls });
beforeEach(() => {
TestBed.configureTestingModule({
providers: [RealService]
});
});
it('should test with real service', () => {
const service = TestBed.inject(RealService);
// real HTTP calls or heavy logic executed
});| Pattern | Test Execution Time | Resource Usage | Network Calls | Verdict |
|---|---|---|---|---|
| Real service in tests | Longer due to real logic | Higher CPU and memory | Possible real HTTP calls | [X] Bad |
| Mocked service in tests | Short and fast | Low CPU and memory | No network calls | [OK] Good |