【发布时间】:2018-11-29 11:45:49
【问题描述】:
我们使用 Angular 5 并将其更新为 7。 目前 store select 写入 pipe();
一些单元测试失败了:
export class MyGuard {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
this.store.dispatch(new Action());
return this.store.pipe(
select<IAppState>(getMainState),
map((state: IState) => {
this.router.navigate(['/', url]);
}),
catchError((a, b) => {
this.router.navigate(['/', 'error']);
return of(false);
}),
);
}
和单元测试:
it('should redirect to page ', inject(
[MyGuard, Service],
(guard: MyGuard, service: Service) => {
spyOn(service, 'method').and.callFake(b => of({ id: { status: 'ok'} }));
const route = new ActivatedRouteSnapshot();
route.params = { id: '2' };
const activation = guard.canActivate(route, {
root: null,
url: 'url',
});
if (activation instanceof Observable) {
activation.subscribe(activationValue => {
expect(activationValue).toBe(false);
expect(router.navigate).toHaveBeenCalled();
});
} else {
fail('should not happen - should return an observable with true');
}
},
));
我们会失败,因为激活 (guard.canActivate) 不是 Observable。可以帮助并告诉如何根据新的 ngrx 重构单元测试吗?
【问题讨论】:
标签: angular unit-testing redux karma-jasmine ngrx