测试NgRx 6效果

时间:2018-10-06 12:10:24

标签: karma-jasmine ngrx-effects angular-testing

我正在尝试在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文档中的一篇。是否有任何要求使用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);
});

});

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

3 个答案:

答案 0 :(得分:0)

预期会有错字。应该是“ b”而不是“ a”

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

答案 1 :(得分: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
  ) {}
}

答案 2 :(得分: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));
相关问题