Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mock catchError #227

Open
ascdi opened this issue Jun 16, 2022 · 1 comment
Open

How to mock catchError #227

ascdi opened this issue Jun 16, 2022 · 1 comment

Comments

@ascdi
Copy link

ascdi commented Jun 16, 2022

i am trying to test a path that leads into an rxjs catchError function.

Example Code:

    return next.handle(request).pipe(
      catchError((error: HttpErrorResponse) => {
        console.log(`test`);

Now i am Mocking the next Part using mock & instance and tried both thenThrow and thenReturn(throwError( in Order to throw an error that could be catched by catchError. But i never reach the console.log statement.

Example:

    when(mockHttpHandler.handle(anything())).thenReturn(
      throwError(
        new HttpErrorResponse({
          error: 'some cryptic error called 93457tz9w345hzg9w24phg9ow4jh',
          url: testUrl,
          status: 500,
        })
      )
    );

How can i reach that path, mocking my next.handle?

@DKozachenko
Copy link

I suppose, I had the same problem.
I have a simple http service, that has method getHeroes. If request is successfull, it returns array with objects. If request failed, it returns empty array. The code of this method is:

getHeroes(): Observable<Hero[]> {
    return this.http.get<Hero[]>(this.heroesUrl).pipe(catchError(() => of([])));
}

The code of test case for situation, when request failed is:

it('should return empty array via "getHeroes" method if request has failed', () => {
   const mockError = new HttpErrorResponse({
     url: 'api/heroes',
     status: 500,
     statusText: 'server error',
   });
   when(mockHttp.get('api/heroes')).thenReturn(throwError(() => mockError));
   const service = createService();

   expect(service.getHeroes()).toBeObservable(
     cold('(a|)', {
       a: [],
     })
   );
});

As you can see I used throwError operator, also I added a | symbol of marble syntax to test has passed.
I'm not sure, it's a best way to solve the problem, but it's ONLY option (I tried a lots of variants over 1,5-2 hours!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants