我有简单的监护人测试,并向我抛出以下错误:
TypeError: Cannot read property 'add' of undefined
我不明白我在做什么错。 您能告诉我我做错了什么,并向我推荐任何ngrx弹珠教程吗?
我的测试和监护人代码:
const store: Store<OrderState> = mock(Store);
let guardian: InitVehicleGuardian;
fdescribe('InitVehicleGuardian', () => {
it('should be created', () => {
guardian = new InitVehicleGuardian(instance(store));
expect(guardian).toBeTruthy();
});
fit('when vehicles loaded then return true', () => {
when(fromStore.getLoadedVehiclesFlag).thenCall(
() => hot('a', {a: true})
);
guardian = new InitVehicleGuardian(instance(store));
const expected = cold('a', { a: true });
expect(guardian.canActivate()).toBeObservable(expected);
});
});
和监护人
@Injectable()
export class InitVehicleGuardian implements CanActivate {
constructor(private store: Store<OrderState>) {
}
public canActivate(): Observable<boolean> {
return this.checkStore().pipe(
switchMap(() => of(true)),
catchError(() => {
return of(false);
}
)
);
}
private checkStore(): Observable<boolean> {
return this.store.pipe(
select(fromStore.getLoadedVehiclesFlag),
tap(loaded => {
if (!loaded) {
this.store.dispatch(new LoadVehicles());
}
}),
filter(loaded => loaded),
first()
);
}
}