I am currently working on creating test cases for the services in my Angular application and encountering some challenges.
Below is the code snippet for the service:
/**
* Sends http request to fetch client states and territories available for a specific vertical
*
* @param {number} vertical id of the specific vertical. Mapping:
* 0 - Federal
* 1 - State
* 2 - Local
* 3 - Education
* @returns {Observable<State[]>} an array of state objects with display name and value as observable
*/
getClientsStatesByVertical(vertical: VerticalEnum): Observable<State[]> {
let params = new HttpParams();
if (vertical != null) {
params = params.append('vertical', String(vertical));
}
return this.http
.get(`${CLIENT_API_ENDPOINT}/csr/states`, {params: params}) as Observable<State[]>;
}
And here is the test I have written:
it('#getClientsStatesByVertical with specified vertical', (done) => {
const expectedResponse = [{label: 'Georgia', value: 'GA'}];
server.respondWith('GET', `${CLIENT_API_ENDPOINT}/csr/states?vertical=${VerticalEnum.Federal}`);
const observable = service.getClientsStatesByVertical(VerticalEnum.Federal);
observable.subscribe(response => {
expect(response).toEqual(expectedResponse);
done();
});
server.respond();
});
Upon running this test using Karma, I encountered the following error:
HttpErrorResponse: Fake server request processing threw exception: Http failure response for http://localhost:19000/api/v1/clients/csr/states: 404 Not Found
I would appreciate your assistance in identifying the issue as it seems like a simple oversight, especially since similar cases with shorter URLs are functioning correctly.