如何在 ngrx/store 测试中 return 特定选择器的 fake/stub 值?

How to return a fake/stub value for a particular selector in ngrx/store testing?

[使用 angular 和 ngrx。]

我正在使用 jest 和 jest-marbles 来测试服务。 我正在使用此服务中的商店。 该服务取决于商店中的布尔值 (isDisplayItems)。

如果为真,则服务中的其他功能才能正常工作。

所以我需要模拟一个值,这样我才能让它工作。 只有我能提供这样的假值才有可能。

jest.spyOn(mockStore.pipe(select(s => s.isDisplayItems))).and.returnValue(true)

这里的想法是 return 特定选择器的 fake/stub 值,以便我可以测试服务。

  it('should make items visible', () =>
  {
    jest.spyOn(mockStore.pipe(select(s => s.isDisplayItems))).and.returnValue(true)

  });

我怎样才能做到这一点?

这是我的测试床设置

  beforeEach(() =>
  {
    TestBed.configureTestingModule({
      providers: [provideMockStore({ initialState }),],
    });
    service = TestBed.inject(FilterService);
    mockStore = TestBed.inject(MockStore);
  });

请按照 ngrx 文档如何测试它们:https://ngrx.io/guide/store/testing#using-mock-selectors

mockStore.overrideSelector

mockStore.overrideSelector(
  yourSelectorHere,
  valueYouWantToReturnAsResult
);

例如

mockStore.overrideSelector(
  currentUser,
  null,
);

this.store.select(currentUser).subscribe(console.log); // prints null.