我正在使用ngrx库并且有这样的效果
@Effect()
loadCollection$: Observable<Action> = this.actions$
.ofType(authAction.GET_USER)
.startWith(new authAction.GetUserAction()) // call on app load
.switchMap(() =>
this.service.getUser()
.mergeMap((user: User) => [
new authAction.GetUserCompleteAction(user),
new navigationAction.GetLinksCompleteAction(user.role)
])
);
我正在为它编写规范,看起来像这样
actions = new ReplaySubject(2);
actions.next(new auth.GetUserAction());
effects.loadCollection$.subscribe(result => {
expect(service.getUser).toHaveBeenCalled();
expect(result).toEqual(new navigation.GetLinksCompleteAction('test')); --> this line fails
});
我怎么能期望在合并图中调用多个动作。
答案 0 :(得分:18)
您可以使用jasmine-marbles
来测试mergeMap
等条件。有关示例,请参阅@ngrx/effects
测试文档:https://github.com/ngrx/platform/blob/master/docs/effects/testing.md
在您的情况下,测试看起来像这样:
actions = hot('-a', { a: new authAction.GetUserAction() });
const expected = cold('-(bc)', { // the ( ) groups the values into the same timeframe
b: new authAction.GetUserCompleteAction({}), // put whatever mock value you have here
c: new navigationAction.GetLinksCompleteAction('test')
};
expect(effects.loadCollection$).toBeObservable(expected);
然后,我会将检查expect(service.getUser).toHaveBeenCalled();
的测试拆分为单独的测试用例。
请参阅https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md了解hot/cold
语法。