簡體   English   中英

未捕獲的TypeError:無法讀取未定義引發的屬性'coSearchCriteria'-Angular Karma / Jasmine

[英]Uncaught TypeError: Cannot read property 'coSearchCriteria' of undefined thrown - Angular Karma/Jasmine

我嘗試使用Karma / Jasmine測試Angular組件。 坦率地說,我對業力/茉莉花沒有太多了解,並且在測試諸如“未捕獲的TypeError:無法讀取未定義的拋出的屬性'coSearchCriteria'”之類的錯誤時。 但是正常的組件功能運行良好。 如果有什么想法請幫助我。

這是我的測試代碼,這里基本的“應該創建”測試用例正在工作,但是第二個給出了錯誤。

 describe('SearchPanelComponent', () => { let component: SearchPanelComponent; let fixture: ComponentFixture<SearchPanelComponent>; beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ReactiveFormsModule, FormsModule, HttpModule, CarbonDatepickerModule, CarbonModalModule,CarbonIconModule, StoreModule.forRoot({})], declarations: [ SearchPanelComponent, UploadsearchcriteriaComponent ], providers: [ Store, StoreModule, CustomerorderService,ConnectionBackend, ApiConnectorService, HttpClient, HttpHandler,Http, CarbonModalService] }) .compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(SearchPanelComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); it('RSSD To should be invalid', async( () => { const dateValue = component.orderUnitForm.controls['rssddateto']; dateValue.setValue('12-02/2012'); fixture.detectChanges(); expect(component.orderUnitForm.valid).toEqual(false); })); 

我只在.ts文件中使用過這樣的'coSearchCriteria'

 this.store.select(selectorCOCriteriaState) .subscribe((coSearchCriteria: SearchCriteriaState) => { this.searchState = coSearchCriteria.lastUsedCriteria; }); if(this.searchState){ this.fillSearchStateData(); } 

終於得到了解決,只需要在spec文件中模擬ngrx存儲。

 class MockStore { public dispatch(obj) { console.log('dispatching from the mock store!') } public select(obj) { console.log('selecting from the mock store!'); return Observable.of({}) } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM