First off, I am still learning the basics of Angular 4 as part of an internship I'm doing; this is all still new to me.
Anyway, I want to simulate an error to an actual HTTP request I'm making in the controller of an Angular component where upon doing so, I can check to see if a Bootstrap error alert appears. (let's assume it's in a div with id "updateErrorMessage") Rather than programming the backend to immediately return an error response, I want to use the Jasmine and Karma testing framework to mock it.
I can't say exactly what the component is, but a more generic one would be, for example, making a PUT request to update the individuals table on a Salesforce Industries database with JSON data from a component called "UpdateIndividualsPage."
Based on this Salesforce developer question here, and this documentation page, the boilerplate code for making the request within a component function is something like:
this.http.post("https://na7.salesforce.com/services/apexrest/clinic01/v1/individual/", this.myJSON, options).map(response => {
}).catch((error: any) => {
}).subscribe();
where upon the response holding an error, I would set
errorOccurredUponUpdatingIndividualsDatabase = true
assuming 'errorOccurredUponUpdatingIndividualsDatabase' is a property in the UpdateIndividualsPage component.
In the updateindividualspage.component.spec.ts file, nested in a describe function call, I would have the boilerplate of a test case be something like:
it('should display update error message upon sending improper data to Salesforce database', () => {
});
Essentially, the last function call in my test would be,
expect(root_Element_Of_Component.querySelector('#updateErrorMessage').innerText).toBe('Error occurred upon updating the Salesforce Industries individuals database.');
I did read some other Stackoverflow questions about creating a mock HTTP error, but I'm not entirely sure, because again, I am still new to this.
Jasmine/Karma testing Angular 5 HTTP responses
mock http request in Angular 4
In Angular 5 testing, how to mock an http error?
I did try something similar to the last test case in the initial code given in this StackOverflow question, except I am only getting "Failed: Expected one matching request for criteria ...", meaning that I have a URL mismatch.
Is there a way for this test case to send an actual HTTP request to that server, but this test framework creates a mock error response? Or do I actually need to create a MockBackend instance and have it send the error, as an HTTP response, back to the frontend, to raise the flag on errorOccurredUponUpdatingIndividualsDatabase to display the Bootstrap alert?
The backend is already implemented, by the way.