Angular Unit Testing NullInjectorError: R3InjectorError

Discussão em 'Angular' iniciado por s1auka, Março 18, 2025.

  1. s1auka

    s1auka Guest

    In my MainComponent I have entryComponent MatSnackBarComponent(custom component). Tests are reporting 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?

    Continue reading...

Compartilhe esta Página