测试效果会抛出NgRx

时间:2018-06-17 10:07:52

标签: angular ngrx

我有效果

  @Effect()
  createNonce$: Observable<Action> = this.actions$.pipe(
    ofType(INVALID_SESSION),
    mergeMap(() =>
      of(generateNonce(32)).pipe(map(nonce => ({ type: REDIRECT_TO_LOGIN, payload: nonce })))
    ),
    catchError(error => of({ type: REDIRECT_TO_LOGIN_FAILURE, payload: error }))
  );

我喜欢在效果抛出时抛出的资产。我正在尝试使用

 it('should dispatch the REDIRECT_TO_LOGIN_FAILURE action if the effect throws', () => {
      const { effects, actions } = setup({});
      spyOn(generateNonce, 'default').and.callFake(() => new Error());

      actions.next({ type: INVALID_SESSION, payload: null });

      effects.createNonce$.subscribe(result => {
        expect(result).toThrowError();
      });
    });

然而,我在控制台中获得的是一个失败的测试和[object ErrorEvent]​​

我的整个spec文件看起来像

import { Router } from '@angular/router';
import { Location } from '@angular/common';
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { ReplaySubject, Observable } from 'rxjs';

import AuthEffects from './auth.effects';

import {
  INVALID_SESSION,
  REDIRECT_TO_LOGIN,
  REDIRECT_TO_LOGIN_FAILURE,
} from './auth.constants';

import * as generateNonce from '../../shared/utils/nonce/generate';
import DiscoveryService from '../../shared/services/discovery/discovery.service';

describe('AuthEffects', () => {
  describe('createNonce$', () => {
    it('should generate a nonce', () => {
      const { effects, actions } = setup({});
      const nonceSpy = spyOn(generateNonce, 'default');
      actions.next({ type: INVALID_SESSION, payload: null });

      effects.createNonce$.subscribe(result => {
        expect(nonceSpy).toHaveBeenCalledWith(32);
      });
    });

    it('should dispatch the REDIRECT_TO_LOGIN action with the nonce as payload', () => {
      const { effects, actions } = setup({});
      spyOn(generateNonce, 'default').and.returnValue('abc');
      const expectedResult = { type: REDIRECT_TO_LOGIN, payload: 'abc' };
      actions.next({ type: INVALID_SESSION, payload: null });

      effects.createNonce$.subscribe(result => {
        expect(result).toEqual(expectedResult);
      });
    });

    it('should dispatch the REDIRECT_TO_LOGIN_FAILURE action if the effect throws', () => {
      const { effects, actions } = setup({});
      spyOn(generateNonce, 'default').and.callFake(() => new Error());

      actions.next({ type: INVALID_SESSION, payload: null });

      effects.createNonce$.subscribe(result => {
        expect(result).toThrowError();
      });
    });
  });

  const setup = propOverrides => {
    const actions: ReplaySubject<any> = new ReplaySubject(1);

    TestBed.configureTestingModule({
      providers: [
        AuthEffects,
        provideMockActions(() => actions),
        {
          provide: DiscoveryService,
          useValue: jasmine.createSpyObj('DiscoveryService', ['getServiceUrl']),
        },
        {
          provide: Location,
          useValue: jasmine.createSpyObj('Location', ['path']),
        },
        {
          provide: Router,
          useValue: jasmine.createSpyObj('Router', ['navigate']),
        },
      ],
    });

    const effects: AuthEffects = TestBed.get(AuthEffects);

    return { effects, actions };
  };
});

如何让这种效果抛出然后资产正确的行为?

0 个答案:

没有答案