There is a section on the Angular documentation which explains how to test HTTP requests; using the HttpClientTestingModule
and HttpTestingController
: https://angular.io/guide/http#testing-http-requests
On that section, they show you how to write a test which makes a HTTP request to a 'mock backend' here: https://angular.io/guide/http#expecting-and-answering-requests
Here is the code used from their example:
it('can test HttpClient.get', () => {
const testData: Data = {name: 'Test Data'};
// Make an HTTP GET request
httpClient.get<Data>(testUrl)
.subscribe(data =>
// When observable resolves, result should match test data
expect(data).toEqual(testData)
);
// The following `expectOne()` will match the request's URL.
// If no requests or multiple requests matched that URL
// `expectOne()` would throw.
const req = httpTestingController.expectOne('/data');
// Assert that the request is a GET.
expect(req.request.method).toEqual('GET');
// Respond with mock data, causing Observable to resolve.
// Subscribe callback asserts that correct data was returned.
req.flush(testData);
// Finally, assert that there are no outstanding requests.
httpTestingController.verify();
});
So my question is, what is the point of this kind of testing? AFAIK, it doesn't increase code coverage as it's not ran against a service and it doesn't call a real backend.
Aucun commentaire:
Enregistrer un commentaire