'Jasmine test to cover catchError in service call
I'm trying since many days to write test for catchError in the below function, but nothings seems to work.
getProducts(productCategory: string | undefined, productStatus: string): Observable<IProductListResponseWrapper> {
return this.http.get<IProductListResponseWrapper>(
${environment.products}?
productType=${productCategory}&productStatus=${productStatus},
).pipe(
retry(environment.productRetryCount),
catchError((error) => this.handleError(error,
this.getPageNameFromStatus(productStatus))),
);
}
I tried
it('handle error', fakeAsync(() => {
const mockError = { error: 'someError' } as ErrorEvent;
service.getProducts("1",ProductStatusEnum.LIVE).subscribe(() => {fail });
const req = httpTestingController.expectOne('/assets/data/products.json?productType=1&productStatus=LIVE');
req.error(mockError);
}));
it('handle error', fakeAsync(() => {
const mockError = { error: 'someError' } as ErrorEvent;
service.getProducts("1",ProductStatusEnum.LIVE).subscribe(() => { }, (thrownError) => {
expect(thrownError.error).toEqual(mockError);
});
const req = httpTestingController.expectOne('/assets/data/products.json?productType=1&productStatus=LIVE');
req.error(mockError);
}));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
