首页 > 解决方案 > NullInjectorError:StaticInjectorError(DynamicTestModule)[ToastrService -> InjectionToken ToastConfig]:在 tslint 角度 8

问题描述

当我在 Angular 8 项目中运行单元测试时,我在 ngx-toastr 中发现了一个错误

NullInjectorError:StaticInjectorError(DynamicTestModule)[ToastrService -> InjectionToken ToastConfig]:

我在 spec.ts 文件中导入了所需的模块,并且我在 app.module.ts 中声明了 forRoot()

  beforeEach(async(() => {
TestBed.configureTestingModule({
  imports: [MatTabsModule,
    ReactiveFormsModule,
    MatTooltipModule,
    HttpClientTestingModule,
    RouterTestingModule,
    ToastrModule
  ],
  declarations: [CommunicationComponent],
  providers: [
    ToastrService,
  ]
})
  .compileComponents();

}));

标签: angularunit-testingeslintngx-toastr

解决方案


从'ngx-toastr'导入{ToastrModule};

beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ToastrModule.forRoot()],

    })
      .compileComponents();
  }));

如上所示在导入中添加 ToastrModule.forRoot() ,您的错误可能会得到解决


推荐阅读