无法测试商店选择订阅

时间:2019-05-10 19:23:23

标签: angular redux jasmine

尝试测试此组件ngOnInit()时出现错误“无法读取未定义的属性'subscribe'”

我添加了select.and.callFake(),它在一段时间前解决了此问题,但现在无法正常工作。

ngOnInit() {
        this.store.select(STORE_USER_REPORT_INFO_STATE).subscribe((userReportInfo: any[]) => {
            if(userReportInfo){
                userReportInfo.forEach((data)=> {
                    if(data.InstanceId == this.id) {
                        this.isFavorite = data.IsFavorite;
                    }
                });
            }
        });
    }


describe('OnlineReportPopupComponent', () => {
let component: OnlineReportPopupComponent;
let fixture: ComponentFixture<OnlineReportPopupComponent>;
let mockStoreUserReportAccessObservable: Observable<any>;
let mockStore;
beforeEach(() => {

        mockStore = jasmine.createSpyObj("Store", ["select"]);
        mockStore.select.and.callFake((stateType: string) => {
            switch (stateType) {
                case STORE_USER_REPORT_INFO_STATE:
                    return mockStoreUserReportAccessObservable;
            }
        });

TestBed.configureTestingModule({
            declarations: [ OnlineReportPopupComponent],
            providers: [
                {provide: Store, useValue: mockStore}
            ]
        });
fixture = TestBed.createComponent(OnlineReportPopupComponent);
        component = fixture.debugElement.componentInstance;
fixture.detectChanges();

 });
}

1 个答案:

答案 0 :(得分:0)

好吧,看来现在没有很多开发人员正在使用UT:P

可以通过实例化可观察对象来解决此问题:

mockStoreUserReportAccessObservable = new Observable<any>();