【问题标题】:Testing ng-rx effect with angular fire call使用 angular fire call 测试 ng-rx 效果
【发布时间】:2018-12-10 20:56:27
【问题描述】:

我正在使用 ng-rx 和 AngularFire 开发一个项目。我正在使用自定义令牌登录。这是效果的代码:

@Effect()
custom$ = this.actions$.pipe(
    ofType(fromAuthActions.AuthActionTypes.CustomTokenReceived),
    switchMap((action: fromAuthActions.CustomTokenReceived) => {
      return fromPromise(this.auth.auth.signInWithCustomToken(action.payload.token)).pipe(
        map((info: UserCredential) => new fromAuthActions.LoginSuccess()),
        tap(a => console.log(a)),
        catchError(error => of(new fromAuthActions.LoginFailed(error)))
      );
    })
  );

然后我对该效果进行了单元测试:

....
const angularFireAuthStub = {
  auth: {
    signInWithCustomToken: jasmine.createSpy('signInWithCustomToken').and.returnValue(Promise.resolve({dd: ''}))
  }
};
....
TestBed.configureTestingModule({
  imports: [
    HttpClientTestingModule
  ],
  providers: [
    AuthService,
    provideMockActions(() => actions),
    {provide: AngularFireAuth, useValue: angularFireAuthStub},
    AuthEffects
  ],
});
....
it('should authorize on fb with custom token and return a login successful action', () => {
    const action = new fromActions.CustomTokenReceived({token: '12345'});
    const completion = new fromActions.LoginSuccess();

    actions = hot('-a', {a: action});
    const expected = cold('-b', {b: completion});

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

运行时我得到:

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

我已经通过添加 console.log 调用验证了 signInWithCustomToken 返回预期的模拟值。我想知道为什么在 toBeObservable 期望中没有检测到。有什么想法吗?

【问题讨论】:

    标签: angular promise angularfire2 ngrx-effects


    【解决方案1】:

    似乎无法使用弹珠测试fromPromise Observable.fromPromise empty during unit testing

    所以我将测试更改为:

    it('should authorize on fb with custom token and return a login successful action', () => {
        const action = new fromActions.CustomTokenReceived({token: '12345'});
        const completion = new fromActions.LoginSuccess();
    
        actions = hot('-a', {a: action});
    
        effects.custom$.subscribe(r => expect(r).toEqual(completion));
      });
    

    【讨论】:

      猜你喜欢
      • 2022-08-15
      • 2018-11-24
      • 2021-10-02
      • 2013-08-01
      • 1970-01-01
      • 2022-10-16
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      相关资源
      最近更新 更多