gpt4 book ai didi

Angular 单元测试 NullInjectorError : R3InjectorError(DynamicTestModule)[MatSnackBarComponent -> MatSnackBarComponent]:

转载 作者:行者123 更新时间:2023-12-05 02:39:28 25 4
gpt4 key购买 nike

在我的 MainComponent 中,我有 entryComponent MatSnackBarComponent(自定义组件)。当我编写测试时出现此错误:

NullInjectorError: R3InjectorError(DynamicTestModule)[MatSnackBarComponent -> MatSnackBarComponent]:NullInjectorError:没有 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();
});

我应该怎么做才能解决这个问题?

最佳答案

已解决。需要在provider下面添加代码

{ provide: MatSnackBarComponent, useValue: {} }

关于 Angular 单元测试 NullInjectorError : R3InjectorError(DynamicTestModule)[MatSnackBarComponent -> MatSnackBarComponent]:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69111109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com