Performance: Testing HTTP calls with HttpTestingController
This concept affects the speed and reliability of frontend tests that simulate HTTP calls without real network delays.
Jump into concepts and practice - no test required
it('should fetch data with HttpTestingController', () => { service.getData().subscribe(data => { expect(data).toBeTruthy(); }); const req = httpTestingController.expectOne('/api/data'); req.flush({ key: 'value' }); httpTestingController.verify(); });
it('should fetch data', (done) => {
service.getData().subscribe(data => {
expect(data).toBeTruthy();
done();
});
});| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Real HTTP calls in tests | N/A | N/A | N/A | [X] Bad |
| HttpTestingController mocks | N/A | N/A | N/A | [OK] Good |
HttpTestingController in Angular testing?HttpTestingController in an Angular test?req.request.method output?
const req = httpMock.expectOne('/api/data');
console.log(req.request.method);request property with HTTP method info.req.request.method is 'GET'.Error: Expected one matching request for criteria "Match URL: '/api/items'", found none.
HttpTestingController?