未捕获的TypeError:无法读取未定义引发的属性'coSearchCriteria'-Angular Karma / Jasmine

时间:2018-09-02 16:36:55

标签: 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();  
    }

1 个答案:

答案 0 :(得分:1)

最后得到了修复,只需要模拟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({})
  }
}