我想测试以下角度4中的路由防护:
@Injectable()
export class AdminGuard implements CanActivate, OnDestroy {
@select(isAdminSelector)
public isAdmin$: Observable<boolean>;
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
return new Promise((resolve, reject) => {
let subscription: Subscription = this.isAdmin$.subscribe((isAdmin) => {
if (isAdmin !== undefined) {
resolve(isAdmin);
subscription.unsubscribe();
}
});
});
}
ngOnDestroy() {}
我已经了解到了
describe('canActivate isAdmin', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [NgxDatatableModule, RouterTestingModule, NgReduxTestingModule],
declarations: [AdminGuard],
providers: []
}).compileComponents();
});
it('should return true for admin user', () => {
const isAdminSelectorMock = MockNgRedux.getSelectorStub(isAdminSelector);
isAdminSelectorMock.next(true);
isAdminSelectorMock.complete();
const authGuard = new AdminGuard();
// how do I stub out active route and state arguments???
expect(authGuard.canActivate( ).toEqual(true);
});
});
但是我不知道如何取消活动路由。