我有以下的效果和测试:
LoginRedirect效应
@Effect()
public loginSuccess$: Observable<Action> = this.actions$.pipe(
ofType<LoginSuccess>(AuthActionTypes.LoginSuccess),
map( action => action.payload ),
concatMap( (payload: Client) => [
new SecondAction(payload),
]),
tap(() => this.router.navigate(['/somepage']))
);LoginRedirect效应试验
describe('#loginSuccess$', () => {
it('should trigger SecondAction action and redirect to somepage', () => {
spyOn(effects['router'], 'navigate');
actions = hot('-a-', { a: new LoginSuccess({} as any)});
expected = cold('-b', { b: new SecondAction({} as any)});
expect(effects.loginSuccess$).toBeObservable(expected);
expect(effects['router'].navigate).toHaveBeenCalled();
});
});测试正在如预期的那样通过。我试图为在ngrx路由器存储上发生的一个可以观察到的操作编写相同的测试。举个例子,这是我的效果。
clearError效应
@Effect()
public clearError$: Observable<Action> = this.actions$.pipe(
ofType(ROUTER_NAVIGATION), // <-- not using the conventional
mapTo(new ClearErrorMessage())
);clearError EffectTest
describe('#clearError$', () => {
it('should trigger ClearErrorMessage action', () => {
spyOn(effects['actions$'], 'pipe').and.returnValue(hot('-a', { a: ROUTER_NAVIGATION }));
expected = cold('-b', { b: new ClearErrorMessage() });
expect(effects.clearError$).toBeObservable(expected);
});
});但是,在这里我得到了一个错误:
Expected $.length = 0 to equal 1.
Expected $[0] = undefined to equal Object({ frame: 10, notification: Notification({ kind: 'N', value: ClearErrorMessage({ type: '[Auth] ClearErrorMessage' }), error: undefined, hasValue: true }) }).任何指点都将不胜感激。
发布于 2018-09-20 06:26:37
您还应该像在第一个示例中那样使用actions,而不是使用spyOn(...)
另外,ROUTER_NAVIGATION只是一个字符串,您必须创建一个操作。
describe('#clearError$', () => {
actions$ = hot('-a---', { a: {type: ROUTER_NAVIGATION} });
expected = cold('-b', { b: new ClearErrorMessage() });
expect(effects.clearError$).toBeObservable(expected);
});https://stackoverflow.com/questions/52413854
复制相似问题