【问题标题】:ngrx router-store - testing router navigation effectsngrx router-store - 测试路由器导航效果
【发布时间】:2019-02-24 02:24:23
【问题描述】:

我的效果和测试如下:

登录重定向效果

  @Effect()
  public loginSuccess$: Observable<Action> = this.actions$.pipe(
    ofType<LoginSuccess>(AuthActionTypes.LoginSuccess),
    map( action => action.payload ),
    concatMap( (payload: Client) => [
      new SecondAction(payload),
    ]),
    tap(() => this.router.navigate(['/somepage']))
  );

登录重定向效果测试

  describe('#loginSuccess$', () => {
    it('should trigger SecondAction action and redirect to somepage', () => {
      spyOn(effects['router'], 'navigate');

      actions = hot('-a-', { a: new LoginSuccess({} as any)});
      expected = cold('-b', { b: new SecondAction({} as any)});

      expect(effects.loginSuccess$).toBeObservable(expected);
      expect(effects['router'].navigate).toHaveBeenCalled();
    });
  });

测试按预期通过。我正在尝试为在 ngrx 路由器存储上发生的操作的 Observable 编写相同的测试。例如,这是我的效果。

清除错误效果

  @Effect()
  public clearError$: Observable<Action> = this.actions$.pipe(
    ofType(ROUTER_NAVIGATION), // <-- not using the conventional 
    mapTo(new ClearErrorMessage())
  );

清除错误效果测试

  describe('#clearError$', () => {
    it('should trigger ClearErrorMessage action', () => {
      spyOn(effects['actions$'], 'pipe').and.returnValue(hot('-a', { a: ROUTER_NAVIGATION }));

      expected = cold('-b', { b: new ClearErrorMessage() });

      expect(effects.clearError$).toBeObservable(expected);
    });
  });

但是,这里出现错误:

Expected $.length = 0 to equal 1.
Expected $[0] = undefined to equal Object({ frame: 10, notification: Notification({ kind: 'N', value: ClearErrorMessage({ type: '[Auth] ClearErrorMessage' }), error: undefined, hasValue: true }) }).

任何指针将不胜感激

【问题讨论】:

  • 什么是“ClearErrorMessage()”???
  • @ALGDB,这是内部操作

标签: angular ngrx ngrx-effects angular-unit-test


【解决方案1】:

您还应该使用actions,就像您在第一个示例中所做的那样,而不是spyOn(...)

另外ROUTER_NAVIGATION 只是一个字符串,你必须创建一个动作。

describe('#clearError$', () => {
   actions$ = hot('-a---', { a: {type: ROUTER_NAVIGATION} });   
   expected = cold('-b', { b: new ClearErrorMessage() });
   expect(effects.clearError$).toBeObservable(expected);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-20
    • 2016-11-16
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 2018-01-04
    相关资源
    最近更新 更多