12

When i run the unit testing in angular 8 project, i found an error in ngx-toastr

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

And i imported the requierd modules in the spec.ts file, And also i declared forRoot() in app.module.ts

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

}));

tamilselvan s
  • 1,045
  • 2
  • 11
  • 13

4 Answers4

22

import { ToastrModule } from 'ngx-toastr';

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

    })
      .compileComponents();
  }));

Add ToastrModule.forRoot() in imports as shown above and your error might be resolved

Anmol Narang
  • 532
  • 5
  • 10
3

These approaches did not work for me. I had to provide my own value for the provider. This is what worked for me:

First, I declared my own "dummy" implementation:

const toastrService = {
    success: (message?: string, title?: string, override?: Partial<IndividualConfig>) => { },
    error: (message?: string, title?: string, override?: Partial<IndividualConfig>) => { }
  };

Then, I indicated the value to use in the providers section:

providers: [
        ...
        { provide: ToastrService, useValue: toastrService },
      ],
Rene Enriquez
  • 1,418
  • 1
  • 13
  • 33
1

Change the providers like below

providers: [
  {provide: ToastrService, useClass: ToastrService}
]
Ininiv
  • 1,325
  • 7
  • 12
0

You may forgot to add .forRoot()

Just add this way in your module.ts

ToastrModule.forRoot()

Parth Developer
  • 1,371
  • 1
  • 13
  • 30