代码之家  ›  专栏  ›  技术社区  ›  tlavarea

具有存储调度操作的角度单元测试组件

  •  0
  • tlavarea  · 技术社区  · 4 年前

    onClearSearch() {
      this.store$.dispatch(some action);
    }
    

    尝试对组件进行单元测试并触发存储调度不起作用。我的组件测试如下所示:

    let store: MockStore<State>;
    
    beforeEach(waitForAsync(() => {
      TestBed.configureTestModule({
        ...
        providers: [
          SearchStoreEffects,
          provideMockActions(() => actions$),
          provideMockStore({initialState, selectors: [...]})
        ],
        ...
      }).compileComponents();
    
      ...
      store = TestBed.inject(MockStore);
      fixture.detectChanges();
    }));
    
    it('should clear search value', () => {
      const storeSpy = spyOn(store, 'dispatch').and.callThrough();
      component.onClearSearch();
      fixture.detectChanges();
      expect(storeSpy).toHaveBeenCalledTimes(1);
    });
    

    0 回复  |  直到 4 年前
        1
  •  1
  •   tlavarea    4 年前

        2
  •  0
  •   Owen Kelvin    4 年前

    it('should clear search value', () => {
      const storeSpy = spyOn(component.store$, 'dispatch').and.callThrough();
      component.onClearSearch();
      fixture.detectChanges();
      expect(storeSpy).toHaveBeenCalledTimes(1);
    });