未捕获的类型错误:无法读取未定义的抛角Karma/Jasmine的属性'coSearchCriteria' [英] Uncaught TypeError: Cannot read property 'coSearchCriteria' of undefined thrown - Angular Karma/Jasmine

查看:24
本文介绍了未捕获的类型错误:无法读取未定义的抛角Karma/Jasmine的属性'coSearchCriteria'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Karma/Jasmine测试角度分量。坦率地说,我对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);
}));

和我在.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({})
  }
}

这篇关于未捕获的类型错误:无法读取未定义的抛角Karma/Jasmine的属性&#39;coSearchCriteria&#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆