0

嗨,我是 NGRX 的新手,正在尝试为我创建的效果编写测试,但是看起来效果功能从未真正运行过。

我要测试的效果是:

compelteAuthentication$ = createEffect(() => this.actions$.pipe(
    ofType(fromActions.completeAuthentication),
    switchMap(() => from(this.authService.completeAuthentication()).pipe(
      map(user => fromActions.authenticationSuccess({ value: user.state})),
      catchError(err => of(fromActions.authenticationFailed(err)))
    ))
  ));

目前的测试版本如下:

import { UserEffects } from './user.effects';
import { AuthService } from './../../core/services/auth.service';
import { ApiServiceMock, ApplicationInsightServiceMock, AppSettingsServiceMock, AuthServiceMock, RouterMock, UserServiceMock } from './../../../testing-utils/mocks/service-mocks';
import { TestBed } from '@angular/core/testing';
import { Action } from '@ngrx/store';
import { provideMockActions } from '@ngrx/effects/testing';
import { Observable } from 'rxjs';
import * as userActions from '../actions/user.actions';
import { cold, hot } from 'jasmine-marbles';
import { User } from 'oidc-client';

let actions$ = new Observable<Action>();
let completeAuthSpy: jasmine.Spy;
let userEffects: UserEffects;

beforeEach(() => {
  completeAuthSpy = jasmine.createSpy();

  TestBed.configureTestingModule({
    providers: [
      provideMockActions(() => actions$),
      UserEffects,
      AppSettingsServiceMock,
      // AuthServiceMock,
      ApiServiceMock,
      UserServiceMock,
      RouterMock,
      ApplicationInsightServiceMock,
      {
        provide: AuthService,
        usevalue: {
          completeAuthentication: completeAuthSpy
        }
      }
    ],
  });

  userEffects = TestBed.inject(UserEffects);
});

describe('UserEffects', () => {
  describe('CompleteAuthentication', () => {
    it('Should call success action', () => {
      const userState = new User({
          id_token: 'TestID',
          session_state: 'TestState',
          access_token: 'TestToken',
          refresh_token: 'TestRefreshToken',
          token_type: 'TestTokenType',
          scope: 'TestScope',
          profile: {} as any,
          expires_at: 6,
          state: undefined,
      });

      const outcome = userActions.authenticationSuccess({ value: userState.state });
      const response = cold('-a|', { a: userState });
      const expected = cold('--b', { b: outcome });
      completeAuthSpy.and.returnValue(response.toPromise());

      actions$ = hot('-c', { c: userActions.completeAuthentication });

      expect(userEffects.compelteAuthentication$).toBeObservable(expected);
      expect(completeAuthSpy).toHaveBeenCalled();
    });
  });
});

因此,当运行上述测试时,第一个期望失败并且它返回[]而不是预期的操作。

任何线索我在这里缺少什么?

4

1 回答 1

1

rxjs observables 是惰性的。这意味着代码在订阅时执行。在您的测试expect(userEffects.compelteAuthentication$).toBeObservable(expected);中正在订阅。所以只需交换expects 的顺序,一切都会好起来的

      actions$ = hot('-c', { c: userActions.completeAuthentication });

      expect(userEffects.compelteAuthentication$).toBeObservable(expected);
      expect(completeAuthSpy).toHaveBeenCalled();
于 2020-12-30T18:37:35.103 回答