Тестирование эффекта ng-rx с угловым пожарным вызовом - PullRequest
0 голосов
/ 10 декабря 2018

Я работаю над проектом, использующим 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 }) }).

Я подтвердил, что signInWithCustomToken возвращает ожидаемое смоделированное значение с добавлением вызова console.log.Мне интересно, почему это не обнаружено в ожидании toBeObservable.Есть идеи?

1 Ответ

0 голосов
/ 11 декабря 2018

Похоже, что невозможно проверить fromPromise с использованием шариков Observable.fromPromise пусто во время модульного тестирования

Поэтому я изменил свой тест на:

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