gpt4 book ai didi

javascript - 具有 Store 调度操作的 Angular 单元测试组件

转载 作者:行者123 更新时间:2023-12-05 00:31:13 24 4
gpt4 key购买 nike

我有一个具有以下功能的组件:

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);
});
测试总是失败,测试已被调用零次。我错过了什么?

最佳答案

您需要监视组件实例

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

关于javascript - 具有 Store 调度操作的 Angular 单元测试组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64779607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com