如何在ngrx 4中正确测试效果?

时间:2017-10-17 13:54:54

标签: testing ngrx-effects ngrx-store-4.0

ngrx 3 中有很多教程如何测试效果

但是,我发现只有1或2个 ngrx4 (他们通过 EffectsTestingModule 删除了经典方法),例如the official tutorial

但是,在我的情况下,他们的方法不起作用。

effects.spec.ts (在 src / modules / list / store / list 下面的链接中)

 describe('addItem$', () => {
    it('should return LoadItemsSuccess action for each item', async() => {
      const item = makeItem(Faker.random.word);
      actions = hot('--a-', { a: new AddItem({ item })});

      const expected = cold('--b', { b: new AddUpdateItemSuccess({ item }) });
      // comparing marbles
      expect(effects.addItem$).toBeObservable(expected);
    });
  })

effects.ts (在 src / modules / list / store / list 下面的链接中)

...
 @Effect() addItem$ = this._actions$
    .ofType(ADD_ITEM)
    .map<AddItem, {item: Item}>(action => {
      return action.payload
    })
    .mergeMap<{item: Item}, Observable<Item>>(payload => {
      return Observable.fromPromise(this._listService.add(payload.item))
    })
    .map<any, AddUpdateItemSuccess>(item => {
      return new AddUpdateItemSuccess({
        item,
      })
    });
...

错误

 should return LoadItemsSuccess action for each item
        Expected $.length = 0 to equal 1.
        Expected $[0] = undefined to equal Object({ frame: 20, notification: Notification({ kind: 'N', value: AddUpdateItemSuccess({ payload: Object({ item: Object({ title: Function }) }), type: 'ADD_UPDATE_ITEM_SUCCESS' }), error: undefined, hasValue: true }) }).
            at compare (webpack:///node_modules/jasmine-marbles/index.js:82:0 <- karma-test-shim.js:159059:33)
            at Object.<anonymous> (webpack:///src/modules/list/store/list/effects.spec.ts:58:31 <- karma-test-shim.js:131230:42)
            at step (karma-test-shim.js:131170:23)

注意:效果使用服务,这涉及写入PouchDB。但是,这个问题似乎与此无关  以及效果工作在正在运行的应用中。

完整的代码是一个Ionic 3应用程序,可以找到here(只是克隆,npm i和 npm run test

更新

使用 ReplaySubject 可行,但不适用于热/冷大理石

  const item = makeItem(Faker.random.word);
  actions = new ReplaySubject(1) // = Observable + Observer, 1 = buffer size

  actions.next(new AddItem({ item }));

  effects.addItem$.subscribe(result => {
    expect(result).toEqual(new AddUpdateItemSuccess({ item }));
  });

1 个答案:

答案 0 :(得分:3)

我的问题由@phillipzada在Github issue I posted.

回答

对于稍后查看的人,我在这里报告答案:

使用大理石使用promises时,这似乎是一个RxJS问题。 https://stackoverflow.com/a/46313743/4148561

我确实设法做了一些应该工作的黑客,但是,你需要对服务进行单独的测试,除非你可以更新服务以返回一个observable而不是一个promise。

基本上我所做的是将Observable.fromPromise调用提取到它自己的&#34;内部函数&#34;我们可以模拟以模拟对服务的调用,然后从那里看。

这样你可以在不使用弹珠的情况下测试内部函数_addItem。

<强>效果

import 'rxjs/add/observable/fromPromise';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';

import { Injectable } from '@angular/core';
import { Actions, Effect } from '@ngrx/effects';
import { Action } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';

export const ADD_ITEM = 'Add Item';
export const ADD_UPDATE_ITEM_SUCCESS = 'Add Item Success';

export class AddItem implements Action {
    type: string = ADD_ITEM;
    constructor(public payload: { item: any }) { }
}

export class AddUpdateItemSuccess implements Action {
    type: string = ADD_UPDATE_ITEM_SUCCESS;
    constructor(public payload: { item: any }) { }
}

export class Item {

}

export class ListingService {
    add(item: Item) {
        return new Promise((resolve, reject) => { resolve(item); });
    }
}

@Injectable()
export class SutEffect {

    _addItem(payload: { item: Item }) {
        return Observable.fromPromise(this._listService.add(payload.item));

    }

    @Effect() addItem$ = this._actions$
        .ofType<AddItem>(ADD_ITEM)
        .map(action => action.payload)
        .mergeMap<{ item: Item }, Observable<Item>>(payload => {
            return this._addItem(payload).map(item => new AddUpdateItemSuccess({
                item,
            }));
        });

    constructor(
        private _actions$: Actions,
        private _listService: ListingService) {

    }
}

<强>规格

import { cold, hot, getTestScheduler } from 'jasmine-marbles';
import { async, TestBed } from '@angular/core/testing';
import { Actions } from '@ngrx/effects';
import { Store, StoreModule } from '@ngrx/store';
import { getTestActions, TestActions } from 'app/tests/sut.helpers';

import { AddItem, AddUpdateItemSuccess, ListingService, SutEffect } from './sut.effect';
import { Observable } from 'rxjs/Observable';

import 'rxjs/add/observable/of';

describe('Effect Tests', () => {

    let store: Store<any>;
    let storeSpy: jasmine.Spy;

    beforeEach(async(() => {

        TestBed.configureTestingModule({
            imports: [
                StoreModule.forRoot({})
            ],
            providers: [
                SutEffect,
                {
                    provide: ListingService,
                    useValue: jasmine.createSpyObj('ListingService', ['add'])
                },
                {
                    provide: Actions,
                    useFactory: getTestActions
                }
            ]
        });

        store = TestBed.get(Store);
        storeSpy = spyOn(store, 'dispatch').and.callThrough();
        storeSpy = spyOn(store, 'select').and.callThrough();

    }));

    function setup() {
        return {
            effects: TestBed.get(SutEffect) as SutEffect,
            listingService: TestBed.get(ListingService) as jasmine.SpyObj<ListingService>,
            actions$: TestBed.get(Actions) as TestActions
        };
    }

    fdescribe('addItem$', () => {
        it('should return LoadItemsSuccess action for each item', async () => {

            const { effects, listingService, actions$ } = setup();
            const action = new AddItem({ item: 'test' });
            const completion = new AddUpdateItemSuccess({ item: 'test' });

            // mock this function which we can test later on, due to the promise issue
            spyOn(effects, '_addItem').and.returnValue(Observable.of('test'));

            actions$.stream = hot('-a|', { a: action });
            const expected = cold('-b|', { b: completion });

            expect(effects.addItem$).toBeObservable(expected);
            expect(effects._addItem).toHaveBeenCalled();

        });
    })

})

<强>助手

import { Actions } from '@ngrx/effects';
import { Observable } from 'rxjs/Observable';
import { empty } from 'rxjs/observable/empty';

export class TestActions extends Actions {
    constructor() {
        super(empty());
    }
    set stream(source: Observable<any>) {
        this.source = source;
    }
}

export function getTestActions() {
    return new TestActions();
}