Angular 6 - NullInjectorError: No provider for HttpClient in unit tests
import { TestBed } from '@angular/core/testing';import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';import {HttpClientModule} from '@angular/common/http';import { myService } from './myservice';describe('myService', () => { beforeEach(() => TestBed.configureTestingModule({ imports: [HttpClientTestingModule], providers: [myService] })); it('should be created', () => { const service: myService = TestBed.get(myService); expect(service).toBeTruthy(); }); it('should have getData function', () => { const service: myService = TestBed.get(myService); expect(service.getData).toBeTruthy(); }); });
you should add HttpClient into imports of your module where your component is declared
@NgModule({ declarations: [ MyComponent ], imports: [ BrowserModule, FormsModule, HttpClientModule ], providers: []})export class AppModule { }
import { TestBed } from '@angular/core/testing';import { HttpClientTestingModule } from '@angular/common/http/testing';import { myService } from './myservice';describe('HeaderService', () => { beforeEach(() => TestBed.configureTestingModule({ imports: [ HttpClientTestingModule ], providers: [myService] }));});