效果规格文件中的SnackBar

问题描述 投票:0回答:1

我正在尝试测试我的效果规格文件。我在效果文件中使用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
            }
        });
    }
angular karma-jasmine ngrx ngrx-effects
1个回答
0
投票

您可以使用“ 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);
});

尝试一下是否可行。

© www.soinside.com 2019 - 2024. All rights reserved.