【问题标题】:ngrx effect unit test mulitple actions in mergemapngrx 效果单元测试 mergemap 中的多个操作
【发布时间】:2017-09-22 21:27:14
【问题描述】:

我正在使用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)
        ])
    );

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

 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
        });

我怎么能期望在合并映射中调用了多个操作。

【问题讨论】:

    标签: ngrx


    【解决方案1】:

    您可以使用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);
    

    然后我会将用于检查expect(service.getUser).toHaveBeenCalled(); 的测试拆分为单独的测试用例。

    有关hot/cold 语法,请参阅https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md。

    【讨论】:

    • 谢谢!在我来到这里之前,我找不到任何关于返回多个动作的测试效果。
    猜你喜欢
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2022-01-15
    • 2019-01-31
    • 1970-01-01
    相关资源
    最近更新 更多