Reputation: 193
In my MainComponent I have entryComponent MatSnackBarComponent(custom component). When I'm writing tests i get this Error:
NullInjectorError: R3InjectorError(DynamicTestModule)[MatSnackBarComponent -> MatSnackBarComponent]: NullInjectorError: No provider for MatSnackBarComponent!
describe('MainComponent', () => {
let component: MainComponent;
let fixture: ComponentFixture<MainComponent>;
let orderService: OrderService;
const fakeMatDialogRef = jasmine.createSpyObj(['close']);
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
FormsModule,
ReactiveFormsModule,
HttpClientTestingModule,
TranslateModule.forRoot()
],
declarations: [MainComponent],
providers: [
OrderService,
{ provide: MatDialogRef, useValue: fakeMatDialogRef },
{ provide: MAT_DIALOG_DATA, useValue: {} }
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MainComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
What should I do to fix this problem?
Upvotes: 9
Views: 39923
Reputation: 193
Solved. Need to add code below to providers
{ provide: MatSnackBarComponent, useValue: {} }
Upvotes: 9
Reputation: 796
You need to add the MatSnackBarModule module in your imports.
imports: [
FormsModule,
ReactiveFormsModule,
HttpClientTestingModule,
MatSnackBarModule, //<---Added MatSnackBarModule
TranslateModule.forRoot()
],
Upvotes: 0