How can I mock an Observable.throw in an Angular2 test?

后端 未结 3 991
不思量自难忘°
不思量自难忘° 2021-02-18 21:54

I want to test the error handling in my Angular2 component and therefore want to mock a service to return an Observable.throw(\'error\'). How can that be done using Jasmine and

3条回答
  •  故里飘歌
    2021-02-18 22:24

    Here is my solution for the ones using Rxjs 6

    let mockService = {
      getData: () => {
        return of({data:'any data'});
      }
    }
    
    spyOn(mockService , 'getData').and.callFake(() => {
      return throwError(new Error('Fake error'));
    });
    

提交回复
热议问题