如何在 Angular 组件中模拟 ngrx 动作?

How to mock ngrx action in Angular component?

//初始化函数的组件代码

this.rootStore.dispatch(new action1());

在我调用 'action1Success'

的效果中

我在组件中有用于 action1Success 和订阅功能的管道

//组件代码

this.actionListener$.pipe( ofType(  MyactionTypes.action1Success)).subscribe((success: SuccessObj) => { });

你可以使用 provideMockActions

import { provideMockActions } from '@ngrx/effects/testing';
...
describe('RouterHistoryEffects', () => {
  let actions: ReplaySubject<any>;
  let effects: YourEffects;

  beforeEach(() => {
   TestBed.configureTestingModule({
    providers: [
      provideMockActions(() => actions)
    ]
  });
 ....

你的测试应该是这样的

// dispatch your action

(actions$ as ReplaySubject).next({ type: '[Users] Get Users' })

// subscribe to the Effect stream
effects.getUsers$.subscribe(action => {
 expect(action).toEqual({
  type: '[Users API] action1Success',
  users: [...],
});
});