In my Angular
application, I'm using Observables
in the following way:
getItem(id): Observable<Object> {
return this.myApi.myMethod(...).catch(e => {
/* CATCH BODY */
return Observable.throw(e);
});
}
and I unit test it:
it('getItem(...) should correctly do its job',
inject([MyService], (service: MyService) => {
const spy = spyOn(myApi, 'myMethod').and.returnValue(mockObservable);
const mockData = 'mock'; // can be anything
const mockObservable = Observable.of(mockData);
service.getItems().subscribe((data) => {
expect(data).toEqual(mockData);
});
expect(spy).toHaveBeenCalledWith(...);
})
);
However, I don't know how to unit test the CATCH BODY
, how can I mock the observable
to throw an error?
I know similar questions have been asked already, but my case is slightly different and I couldn't succeed in unit testing the catch part.