ngrx效果单元测试合并映射中的多个动作

Ang*_*gad 10 ngrx

我正在使用ngrx库并且有这样的效果

@Effect()
  loadCollection$: Observable<Action> = this.actions$
    .ofType(authAction.GET_USER)
    .startWith(new authAction.GetUserAction()) // call on app load
    .switchMap(() =>
      this.service.getUser()
        .mergeMap((user: User) => [
          new authAction.GetUserCompleteAction(user),
          new navigationAction.GetLinksCompleteAction(user.role)
        ])
    );
Run Code Online (Sandbox Code Playgroud)

我正在为它编写规范,它看起来像这样

 actions = new ReplaySubject(2);
        actions.next(new auth.GetUserAction());

        effects.loadCollection$.subscribe(result => {
            expect(service.getUser).toHaveBeenCalled();
            expect(result).toEqual(new navigation.GetLinksCompleteAction('test'));  --> this line fails
        });
Run Code Online (Sandbox Code Playgroud)

我怎么能期望在合并映射中调用多个动作.

Sil*_*hus 24

你可以jasmine-marbles用来测试像这样的条件mergeMap.有关@ngrx/effects示例,请参阅测试文档:https://github.com/ngrx/platform/blob/master/docs/effects/testing.md

在您的情况下,测试看起来像这样:

  actions = hot('-a', { a: new authAction.GetUserAction() });

  const expected = cold('-(bc)', { // the ( ) groups the values into the same timeframe
      b: new authAction.GetUserCompleteAction({}), // put whatever mock value you have here
      c: new navigationAction.GetLinksCompleteAction('test')
  };

  expect(effects.loadCollection$).toBeObservable(expected);
Run Code Online (Sandbox Code Playgroud)

然后,我会将测试拆分expect(service.getUser).toHaveBeenCalled();为一个单独的测试用例.

https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.mdhot/cold语法.

  • 谢谢!在我来到这里之前,我找不到任何关于返回多个动作的测试效果的信息。 (2认同)