Angular 角度单元测试NullInjectorError:HttpClient没有提供程序!错误

Angular 角度单元测试NullInjectorError:HttpClient没有提供程序!错误,angular,typescript,httpclient,Angular,Typescript,Httpclient,我知道这个问题已经被反复问过了。我无法找到一个有效的解决方案。我目前正在将单元测试添加到我们的项目中,从而修复所有自动生成的单元测试 但是在运行ng测试时,我得到以下错误: NullInjectorError: R3InjectorError(DynamicTestModule)[Service -> HttpClient -> HttpClient]: NullInjectorError: No provider for HttpClient! 请注意->HttpClient-

我知道这个问题已经被反复问过了。我无法找到一个有效的解决方案。我目前正在将单元测试添加到我们的项目中,从而修复所有自动生成的单元测试

但是在运行ng测试时,我得到以下错误:

NullInjectorError: R3InjectorError(DynamicTestModule)[Service -> HttpClient -> HttpClient]: 
NullInjectorError: No provider for HttpClient!
请注意->HttpClient->HttpClient

当我第一次看到这一点时,我认为这一定是一个循环依赖性问题。这让我创建了一个测试模块,我正在将它导入我的测试床

这是一个失败的样本测试

import {  TestBed } from '@angular/core/testing';

import { SearchListService } from './search-list.service';
import { ServiceTestingModule } from '@app/testing/service.testing.module';
import { ZhwKnowledgeJourneyService } from '@bkh/services/zhw-knowledge-journey.service';


describe('ZhwKnowledgeJourneyService', () => {

  beforeEach(() => TestBed.configureTestingModule({
    imports: [ServiceTestingModule],
    providers: [SearchListService]
  }));

  it('should be created', () => {
    const service: ZhwKnowledgeJourneyService = TestBed.inject(ZhwKnowledgeJourneyService);
    expect(service).toBeTruthy();
  });
});
这是我的“服务测试模块”

我也检查过,“导入”总是在“提供者”之前,但仍然没有运气

我也阅读了(并测试了)所有关于这个主题的Github和Stackoverflow帖子,但因为我在那里运气不好,所以我再次问这个问题


提前感谢

尝试将HttpClientModule添加到提供商

我也刚刚开始使用Angular,遇到了同样的问题。当您的组件/服务使用
HttpClientModule
时,似乎需要在
beforeach
函数中导入
HttpClientTestingModule

下面是一个名为
SituationService

从'@angular/core/testing'导入{TestBed};
从“./situation.service”导入{SituationService};
从'@angular/common/http/testing'导入{HttpClientTestingModule};
描述('SituationService',()=>{
让服务:情景服务;
beforeach(异步()=>{
等待TestBed.configureTestingModule({
进口:[
HttpClientTestingModule
]});
configureTestingModule({});
service=TestBed.inject(情景服务);
});
它('应该创建',()=>{
expect(service.toBeTruthy();
});
});
下面是一个名为
sition

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { SituationComponent } from './situation.component';
import {HttpClientTestingModule} from '@angular/common/http/testing';

describe('SituationComponent', () => {
  let component: SituationComponent;
  let fixture: ComponentFixture<SituationComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ SituationComponent ],
      imports: [
        HttpClientTestingModule
      ]
    })
    .compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(SituationComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});
从'@angular/core/testing'导入{ComponentFixture,TestBed};
从“./situation.component”导入{SituationComponent};
从'@angular/common/http/testing'导入{HttpClientTestingModule};
描述('SituationComponent',()=>{
let组件:情景组件;
let夹具:组件夹具;
beforeach(异步()=>{
等待TestBed.configureTestingModule({
声明:[情景组件],
进口:[
HttpClientTestingModule
]
})
.compileComponents();
});
在每个之前(()=>{
fixture=TestBed.createComponent(情景组件);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
});

请注意每个
函数之前的
导入
部分。

尝试在测试中导入
HttpClientTestingModule
,不要与
HttpClientModule
@MikeS混淆。正如你在我的代码中看到的,我已经做过了。或者你会改变什么?我还尝试直接导入此模块。但是没有运气。
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { SituationComponent } from './situation.component';
import {HttpClientTestingModule} from '@angular/common/http/testing';

describe('SituationComponent', () => {
  let component: SituationComponent;
  let fixture: ComponentFixture<SituationComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ SituationComponent ],
      imports: [
        HttpClientTestingModule
      ]
    })
    .compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(SituationComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});