测试NgRx 6效果

问题描述 投票:1回答:2

我试图在Angular 6项目中测试ngrx效果,我总是得到错误:

Expected $[0].notification.kind = 'C' to equal 'N'.
Expected $[0].notification.hasValue = false to equal true.

我试过这篇文章https://brianflove.com/2018/06/28/ngrx-testing-effects/和ngrx doc中的那篇文章。是否有任何要求使用ngrx 6进行效果测试?这个错误对我来说不够有意义。也许有人有一个关于怎么做的完整例子?

这是我的效果:

initData$: Observable<Action> = this.actions$.pipe(
    ofType(INIT_DATA_ACTION),
    switchMap((data: any) => {
        return this.store.pipe(select(getAppDataResolved)).take(1).switchMap((resolved: any) => {
            if (!resolved) {
                return this.dataService.getInitData(this.loginService.user.id).switchMap((response: any) => {
                    return Observable.from([
                        new ItemsInitDataAction(response.userItems),
                        new InitDataResolvedAction(),
                    ]);
                });
            } else {
                return Observable.from([
                    new InitDataResolvedAction(),
                ]);
            }
        });
    }),
);

和我的业力考验:

it('should be created', () => {
    expect(effects).toBeTruthy(); // got success
});

it('basic test', () => { // got error
    const action = new appAction.InitDataAction();
    const outcome = new appAction.InitDataResolvedAction();

    actions.stream = hot('a', { a: action });
    const expected = hot('a', { b: outcome });

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

});

在此先感谢您的帮助;-)

karma-jasmine ngrx-effects angular-testing
2个回答
0
投票

预计有一个错字。它应该是'b'而不是'a'

const expected = hot('b', { b: outcome });

0
投票

我还没有能够使用弹珠进行测试。

我使用Nrwl nx,所以我的效果测试看起来像这样:

import { TestBed } from '@angular/core/testing';
import { Subject, ReplaySubject } from 'rxjs';
import { EffectsModule } from '@ngrx/effects';
import { StoreModule } from '@ngrx/store';
import { provideMockActions } from '@ngrx/effects/testing';
import { NxModule } from '@nrwl/nx';
import { DataPersistence } from '@nrwl/nx';
import { ChangePasswordEffects } from './change-password.effects';
import { ChangePassword, ChangePasswordSuccessful } from './change-password.actions';
import { HttpClientTestingModule } from '@angular/common/http/testing';

describe('ChangePasswordEffects', () => {
  let actions: Subject<any>;
  let effects: ChangePasswordEffects;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [NxModule.forRoot(), StoreModule.forRoot({}), EffectsModule.forRoot([]), HttpClientTestingModule],
      providers: [ChangePasswordEffects, DataPersistence, provideMockActions(() => actions)]
    });

    effects = TestBed.get(ChangePasswordEffects);
  });

  describe('loadChangePassword$', () => {
    it('should work', () => {
      actions = new ReplaySubject(1);

      actions.next(ChangePassword);

      effects.loadChangePassword$.subscribe(result => {
        expect(result).toEqual(ChangePasswordSuccessful);
      });
    });
  });
});

我的代码看起来像这样:

import { PasswordChangeError } from './../../models/password-change-error';
import { Injectable } from '@angular/core';
import { Effect, Actions } from '@ngrx/effects';
import { DataPersistence } from '@nrwl/nx';
import { ChangePasswordPartialState } from './change-password.reducer';
import {
  ChangePassword,
  ChangePasswordSuccessful,
  ChangePasswordError,
  ChangePasswordActionTypes
} from './change-password.actions';
import { ChangePasswordService } from '../../services/change-password/change-password.service';
import { map } from 'rxjs/operators';

@Injectable()
export class ChangePasswordEffects {
  @Effect() loadChangePassword$ = this.dataPersistence.fetch(ChangePasswordActionTypes.ChangePassword, {
    run: (action: ChangePassword, state: ChangePasswordPartialState) => {
      return this.passwordService
        .changePassword(action.newPassword, action.userId)
        .pipe(map(res => new ChangePasswordSuccessful(res)));
    },

    onError: (action: ChangePassword, error: PasswordChangeError) => {
      return new ChangePasswordError(error);
    }
  });

  constructor(
    private actions$: Actions,
    private dataPersistence: DataPersistence<ChangePasswordPartialState>,
    private passwordService: ChangePasswordService
  ) {}
}

0
投票

我认为你需要为Selector插入一个模拟,控制台的下一行应该描述一些关于缺少选择器数据的东西。

let store: Store<any>

class MockStore {
    select(){}
}

TestBed.configureTestingModule({
   providers: [
     {
       provide: Store,
       useClass: MockStore
     }
   ]
});
store = TestBed.get(Store);

在测试套件中,您可以使用Spy为您提供所需的任何商店:

spyOn(store, 'select').and.returnValue(of(initialState));
© www.soinside.com 2019 - 2024. All rights reserved.