Angular testing and HttpClient: TypeError: Cannot read property 'get' of undefined

3.7k views Asked by At

In my Angular 4 application I need to create a test for a component which uses a service which internally uses the angular HttpClient.

Despite I added HttpClientTestingModule between the imports of my TestBed, I get:

TypeError: Cannot read property 'get' of undefined

whenever the component calls MyService.someMethod() (which internally uses HttpClient.get()).

I tried providing the service itself or providing a mock for it, but I'm getting the error anyway.

1

There are 1 answers

1
Francesco Borzi On BEST ANSWER

I found the problem:

I needed to remove { provide: AuthService, useClass: MockAuthService }, from my TestBed's providers list. It is no longer needed and somehow was interfering with the HttpClientTestingModule.