result after the test runs?let result: any; service.getData().subscribe(data => result = data); const req = httpTestingController.expectOne('/api/data'); req.flush({name: 'Test'}); httpTestingController.verify();
flush sends the mock response to the subscriber.The flush method sends the mock response to the observable, so result receives the object {name: 'Test'}. The expectOne call matches the request, so no error occurs.
expectOne to match a single request and flush to send the response.expectOne matches exactly one request. flush sends the mock response. match returns an array, so calling flush on it causes an error. send is not a method on the request. expect is not a valid method.
httpTestingController.expectOne('/api/users') throw an error?service.fetchUsers().subscribe(); // No HTTP call is made inside fetchUsers httpTestingController.expectOne('/api/users');
expectOne looks for a matching HTTP request. If the service method does not make any HTTP call, expectOne throws an error because it finds no matching request.
errorResponse after this test triggers an HTTP error?errorResponse contain after the HTTP call fails?let errorResponse: any; service.getData().subscribe({ next: () => {}, error: err => errorResponse = err.status }); const req = httpTestingController.expectOne('/api/data'); req.flush('Not Found', {status: 404, statusText: 'Not Found'}); httpTestingController.verify();
The flush method with error status triggers the error callback. The err.status contains the HTTP status code 404.
HttpTestingController intercepts HTTP requests made by HttpClient and lets tests verify and respond to them manually. It does not automatically mock all requests or replace HttpClient entirely.