lundi 19 avril 2021

Testing service with http still gives provider not found while importing HttpClientTestingModule

I'm starting making tests for my Angular project but get stuck with some problems while testing my service which is using HTTP.

Bases on other discussion I've already imported the HttpClientTestingModule but I still get a error that the provider is not found..

My code currently looks like:

import { TestBed } from '@angular/core/testing';
import {
  HttpClientTestingModule,
} from '@angular/common/http/testing';

import { AuthenticationService } from './authentication.service';

describe('AuthenticationService', () => {
  let service: AuthenticationService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
    });
    service = TestBed.inject(AuthenticationService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });
});

And the given error while running the code is:

NullInjectorError: R3InjectorError(DynamicTestModule)[ErrorInterceptor -> AuthenticationService -> HttpClient -> HttpClient]: 
  NullInjectorError: No provider for HttpClient!
    at NullInjector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11049:1)
    at R3Injector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11216:1)
...

I hope someone knows what to do.

Aucun commentaire:

Enregistrer un commentaire