Javascript 具有存储分派动作的角度单元测试组件
我有一个具有以下功能的组件:Javascript 具有存储分派动作的角度单元测试组件,javascript,angular,unit-testing,ngrx,Javascript,Angular,Unit Testing,Ngrx,我有一个具有以下功能的组件: onClearSearch() { this.store$.dispatch(some action); } 尝试对组件进行单元测试并触发存储调度不起作用。我的组件测试如下所示: let store: MockStore<State>; beforeEach(waitForAsync(() => { TestBed.configureTestModule({ ... providers: [ SearchSt
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);
});
let-store:MockStore;
每次之前(waitForAsync(()=>{
TestBed.configureTestModule({
...
供应商:[
搜索效果,
provideMockActions(()=>actions$),
ProvideMacStore({initialState,选择器:[……]})
],
...
}).compileComponents();
...
store=TestBed.inject(MockStore);
fixture.detectChanges();
}));
它('应该清除搜索值',()=>{
const storeSpy=spyOn(store,'dispatch')。和.callThrough();
onClearSearch();
fixture.detectChanges();
预期(storeSpy)。已被催收时间(1);
});
测试总是失败,测试被称为零次。我缺少什么?您需要监视组件实例
it('应清除搜索值',()=>{
const storeSpy=spyOn(component.store$,'dispatch')。和.callThrough();
onClearSearch();
fixture.detectChanges();
预期(storeSpy)。已被催收时间(1);
});
如果我用fakeAsync包装it断言,然后在component.onClearSearch()之后调用tick;,它工作错误消息是什么?同样的,“预期spy dispatch已被调用一次。它被调用了0次。”如果我将it断言包装为fakeAsync,则在component.onClearSearch()之后调用tick;,它起作用了