mercredi 10 mars 2021

Using catchError, throwError in Jasmine

In my angular project there is a service with a method

import { throwError as observableThrowError, Observable } from 'rxjs';   
import { catchError } from 'rxjs/operators';

getUser(): Observable<User> {
  return <Observable<User>>this.http
  .get(`${baseUrl}/api/v1/user`)
  .pipe(
    catchError((error: any) => observableThrowError(error) )
  );
}

In the unit test I check request to expected URL:

it('should have made request to getUser from expected url', () => {
  const expectedUser = { username: 'user' };

  apiService.getUser().subscribe(user => {
    expect(user).toEqual(expectedUser);
  });

  const req = httpTestingController.expectOne(`${baseUrl}/api/v1/user`);
  expect(req.request.method).toEqual('GET');
  req.flush(expectedUser);
});

..and it works. But I don't know how to check if the error:

enter image description here

Any ideas?

Aucun commentaire:

Enregistrer un commentaire