s1auka
s1auka

Reputation: 193

Angular Unit-testing NullInjectorError: R3InjectorError(DynamicTestModule)[MatSnackBarComponent -> MatSnackBarComponent]:

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

Answers (2)

s1auka
s1auka

Reputation: 193

Solved. Need to add code below to providers

{ provide: MatSnackBarComponent, useValue: {} }

Upvotes: 9

dom.macs
dom.macs

Reputation: 796

You need to add the MatSnackBarModule module in your imports.

imports: [
        FormsModule,
        ReactiveFormsModule,
        HttpClientTestingModule,
        MatSnackBarModule, //<---Added MatSnackBarModule
        TranslateModule.forRoot()
],

Upvotes: 0

Related Questions