我想测试一个返回可观察响应的守卫。这是我的警卫代码:
awk -F "\t" '{ (if($4 < -10 & $5==BA)), (if($4 < -5 & $5==CA)) ; print $2 = BA/CA} file.txt > out.txt
我的测试代码(注入所需的服务)
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { UserService, User } from '@app/user';
@Injectable()
export class SignGuard implements CanActivate {
constructor(private router: Router, private userService: UserService) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.userService.get().map((user: User) => {
if (user.isPremium()) {
this.router.navigate([state.url, 'success']);
return false;
} else {
return true;
}
});
}
}
测试用例
beforeEach(inject([SignGuard, Router], (guard: SignGuard, r: Router) => {
signGuard = guard;
activatedSnapshot = new ActivatedRouteSnapshot();
router = r;
}));
有什么想法吗?