我试图使用Karma / Jasmine测试Angular组件 . 坦率地说,我对karma / jasmine并没有太多了解,同时测试得到的错误如“未捕获的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);
}));
'coSearchCriteria'我在.ts文件中使用的只是这样
this.store.select(selectorCOCriteriaState)
.subscribe((coSearchCriteria: SearchCriteriaState) => {
this.searchState = coSearchCriteria.lastUsedCriteria;
});
if(this.searchState){
this.fillSearchStateData();
}
1 回答
终于得到了修复,只需要在spec文件中模拟ngrx存储 .