Angular 4 Error: No provider for HttpClient

二次信任 提交于 2019-11-30 06:04:43

In your test

TestBed.configureTestingModule({
      providers: [FlexSearchService, HttpClientModule]
    });

It should be

TestBed.configureTestingModule({
      imports: [HttpClientModule],
      providers: [FlexSearchService]
    });

or even better (if you want to mock request):

TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [FlexSearchService]
    });

An easier way is to provide it globally....Import the following into app.module.ts like so:

import { HttpModule } from '@angular/http'
import { HttpClient, HttpClientModule } from '@angular/common/http';

and declare it in imports:

  imports: [
    HttpModule,
    HttpClientModule, ...
]

Import HttpClientTestingModule.

In your test:

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

and in the configureTestingModule of your test, do the following:

TestBed.configureTestingModule({
    imports: [ HttpClientTestingModule ],
})
.compileComponents();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!