tommueller
tommueller

Reputation: 2496

HttpClient testing issue: No provider for HttpClient with nested services

I have a problem upgrading to HttpClient from Http in Angular 4. I have a service which has a nested reference to HttpClient. My structure looks like this: My TokenHandlerService injects MyHttpService which injects Angulars HttpClient

Running the code snippets I found online looks like this:

let service: TokenHandlerService;
let httpMock: HttpTestingController;

beforeEach(() => {
    TestBed.configureTestingModule({
        imports: [
            HttpClientModule,
            HttpClientTestingModule
        ],
        providers: [
            TokenHandlerService,
            MyHttpService
        ]
    });
    httpMock = TestBed.get(HttpTestingController);
    service = TestBed.get(TokenHandlerService);
});

I want to test the TokenHandlerService, but running this I always end up in an Error: No provider for HttpClient!.

How can I pass the mocked HttpClient to deeper nested services?

Upvotes: 1

Views: 1000

Answers (2)

tommueller
tommueller

Reputation: 2496

Thanks to the support team at github I was able to fix this by adding:

import 'core-js/es7/reflect';

to my test.ts file.

Upvotes: 1

Borad Akash
Borad Akash

Reputation: 804

Can you try removing HttpClientModule from import statement?

because we already added HttpClientTestingModule for our test module

Upvotes: 0

Related Questions