Angular Ngrx 测试:Store.overrideSelector 不会覆盖

tho*_*omi 8 unit-testing ngrx angular angular-test

我确实有以下内容beforeEach

beforeEach(() => {
    fixture = TestBed.createComponent(ApplyComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    store = TestBed.get<Store<fromAnnouncement.AnnouncementState>>(Store);
    store.refreshState();
    mockApplicationSelector = store.overrideSelector(selectDraftApplication, testDraftApplication); <-- Try to mock  a selector here.
  });
Run Code Online (Sandbox Code Playgroud)

选择器 selectDraftApplication 取决于 的功能选择器AnnouncementState,其中有两个实体,其中之一是我使用功能选择器进行子选择以构建我的selectDraftApplication选择器。

当在测试中使用这段代码时,我所有的测试都会崩溃,因为 NgRx 似乎仍然在寻找进一步向上的所有状态,而我希望模拟能够准确地防止这种情况发生。模拟整个 ngrx 实体存储是没有意义的,所以我只希望选择器准确地返回该对象并完成它。我用谷歌搜索了很远很远,但没有得到任何答案。有什么帮助吗?我运行 Angular 8 和 ngrx 8.5.1(该refreshState()功能也不起作用......)

谢谢!

编辑

export const getAnnouncementState = createFeatureSelector<AnnouncementState>('announcement');

export const getApplicationsState = createSelector(
  getAnnouncementState,
  (state: AnnouncementState) => state.applications 
);

export const selectDraftApplication = createSelector(
  getApplicationsState,
  (state: ApplicationState) => state.draftApplication
);
Run Code Online (Sandbox Code Playgroud)

GKA*_*GKA 6

您需要refreshState()在模拟选择器后调用...

beforeEach(() => {
    fixture = TestBed.createComponent(ApplyComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    store = TestBed.get<Store<fromAnnouncement.AnnouncementState>>(Store);
    //
    mockApplicationSelector = store.overrideSelector(selectDraftApplication, testDraftApplication); <-- Try to mock  a selector here.

    store.refreshState(); <-- refresh state ***after*** mocking the selector
  });
Run Code Online (Sandbox Code Playgroud)