I want to test the error part of the http call in the myService.ts
file.
private executeSend(id: string): void {
this.conService.createContact(id).subscribe(resp => {
// some coding
},
err => { // need for unit test
if(err.status === 504) {
// some coding
}
});
}
I wrote the unit testing as like below but it shows me: Cannot read properties of undefined (reading 'createContact')
it('should check 504 error for executeSend method', () => {
const errorResponse = new Response("err", {
status: 504
});
const fetchSpy = jasmine.createSpy('post').and.returnValue(errorResponse);
const op = new service['myService']['createContact'](fetchSpy);
let error;
try {
op.createContact()
} catch (e) {
error = e;
}
expect(error).toEqual(new Error('err'));
expect(fetchSpy).toHaveBeenCalled();
});