如何使用 MockStore 和 MemoizedSelector 在 Angular 8 单元测试中模拟 @ngrx/store 状态

Jit*_*dra 9 unit-testing mocking ngrx ngrx-store angular8

我正在尝试使用 RXJS 在 Angular 8 中使用 jest 创建单元测试。我想模拟商店。我正在使用https://ngrx.io/guide/store/testing使用模拟选择器的示例。谁能帮助我如何使用 MemoizedSelector 和 MockStore 独立使用模拟商店。

 this.store.select(homeState).pipe(select(s => s.checkStatus)).subscribe(status => {
     console.log(status);
     // We performation other action.
});
Run Code Online (Sandbox Code Playgroud)

我在这个组件中有很多选择器。如何模拟许多选择器并更新每个测试用例的选择器值?

sat*_*ime 9

您应该按照使用模拟选择器中的信息进行操作。

describe('User Greeting Component', () => { 
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [provideMockStore()],
      declarations: [YourComponent],
    }); 
  });

  it('test', () => {
    const mockStore = TestBed.inject(MockStore);
    const mockHomeState = mockStore.overrideSelector(
      homeState,
      {checkStatus: true}
    );

    const fixture = TestBed.createComponent(YourComponent);
    fixture.detectChanges();
  });
});
Run Code Online (Sandbox Code Playgroud)