我正在尝试测试我的效果规格文件。我在效果文件中使用matSnackBar。当我运行它时,_snackbar声明为undefined,然后是tests字段。这就是我试图做的事情:
describe('InquiryWizardEffects', () => {
let actions: Observable<any>;
let effects: InquiryWizardEffects;
let inquiryService: MockInquiryService;
let _snackBar: MatSnackBar;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
NxModule.forRoot(),
StoreModule.forRoot({}),
EffectsModule.forRoot([]),
MatSnackBarModule,
],
providers: [
InquiryWizardEffects,
DataPersistence,
provideMockActions(() => actions),
{ provide: InquiriesService, useClass: MockInquiryService },
{ provide: MatSnackBar}
]
});
_snackBar= TestBed.get(MatSnackBar);
effects = TestBed.get(InquiryWizardEffects);
inquiryService = TestBed.get(InquiriesService);
});
我在做什么错?
这是我得到的错误:
'无法读取未定义的属性'openFromComponent''
当我在做的时候来:
this._snackBar.openFromComponent(CreatedEntitySnackBarComponent, {
duration: environment.longDurationSnackBar,
panelClass: [style],
horizontalPosition: 'right',
data: {
title: title,
entityId: entityId
}
});
}
您可以使用“ useValue”提供SnackBar并进行伪造。
describe('InquiryWizardEffects', () => {
let actions: Observable<any>;
let effects: InquiryWizardEffects;
let inquiryService: MockInquiryService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
NxModule.forRoot(),
StoreModule.forRoot({}),
EffectsModule.forRoot([]),
MatSnackBarModule,
],
providers: [
InquiryWizardEffects,
DataPersistence,
provideMockActions(() => actions),
{ provide: InquiriesService, useClass: MockInquiryService },
{ provide: MatSnackBar, useVale: {openFromComponent: (param1, param2) => { return; }}},
]
});
effects = TestBed.get(InquiryWizardEffects);
inquiryService = TestBed.get(InquiriesService);
});
尝试一下是否可行。