组件

时间:2018-12-14 10:57:54

标签: ngrx

我在ngOnInit中有一个带有实体选择器'selectAllProperties'的组件,我想测试该组件:

ngOnInit() {
    this.store.dispatch(new LoadPropertiesRequested());
    this.properties$ = this.store.pipe(select(selectAllProperties));
    this.loading$ = this.store.pipe(select(selectPropertiesLoading));
    this.logs$ = this.store.pipe(select(selectPropertiesLogs));
  }

在我的spec文件中,我像在ngrx doc中一样初始化了商店:

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        StoreModule.forRoot({
          ...fromRoot.reducers,
          feature: combineReducers(fromProperties.reducer),
        })
      ],
      declarations: [
        SidebarPropertiesComponent,
        SidebarElementComponent
      ]
    })
    .compileComponents();
  }));
  • 启动测试时,出现“ TypeError:无法读取未定义的属性'ids'”。 所有其他选择器都不会产生错误

  • 我也想模拟每个选择器返回的Observable。

谢谢

1 个答案:

答案 0 :(得分:1)

我在TestBed.configureTestingModule

中发现了问题

代替

  imports: [
    StoreModule.forRoot({
      ...fromRoot.reducers,
      feature: combineReducers(fromProperties.reducer),
    })
  ],

使用

  imports: [
    StoreModule.forRoot(reducers, { metaReducers }),
    StoreModule.forFeature('properties', fromProperties.reducer),
  ],
  • 不再有'TypeError:无法读取未定义的属性'ids'
  • 我可以模拟属性
 it('should have 2 properties elements', () => {
   store.dispatch(new LoadPropertiesSuccess({properties: propertiesMock}));
   fixture.detectChanges();

   const list = debugElement.queryAll(By.css('li'));
   expect(list.length).toBe(2);
 });