我正在实现一个Angular 为15.2.9的functional router guard,它判断用户是否已登录.如果不是这样,警卫应该返回false
或UrlTree
(即重定向到登录页面),具体取决于参数redirectToLogin: boolean
.
我正在使用一个工厂函数来实现功能路由守卫,它类似于百分之一:
export const isLoggedIn = (redirectToLogin: boolean): CanActivateFn => {
return (next: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {
const isLoggedIn = !!inject(CookieService).check('some_cookie');
if (isLoggedIn) return true;
if (!isLoggedIn && !redirectToLogin) return false;
const redirectUrl = inject(Router).createUrlTree(['user', 'sign_in'], {
queryParams: { next: encodeURIComponent(`/${next.url.join('/')}`) },
queryParamsHandling: 'merge',
});
return redirectUrl;
};
};
它像预期的那样工作,但我不能为它写specs:
let cookieServiceSpy;
let activatedRouteSnapshot;
beforeEach(() => {
cookieServiceSpy = jasmine.createSpyObj<CookieService>(['check']);
activatedRouteSnapshot = { url: [], data: {} };
TestBed.configureTestingModule({
providers: [
isLoggedIn,
{ provide: CookieService, useValue: cookieServiceSpy },
{
provide: ActivatedRouteSnapshot,
useValue: activatedRouteSnapshot,
},
],
});
});
it('should return true when secure_user_id cookie is present and redirectToLogin is false', () => {
// given
cookieServiceSpy.check.and.returnValue(true);
activatedRouteSnapshot.data.redirectToLogin = false;
// when
const result = TestBed.runInInjectionContext(isLoggedIn); // <-- this line is the problem
// then
expect(result).toBeTrue();
});
导致打字错误:Argument of type '(redirectToLogin: boolean) => CanActivateFn' is not assignable to parameter of type '() => CanActivateFn'. Target signature provides too few arguments. Expected 1 or more, but got 0.
我还try 了其他几种 Select :
-
TestBed.runInInjectionContext(isLoggedIn as any)
个➔Can't resolve all parameters for isLoggedIn: (?).正在试运行 -
TestBed.runInInjectionContext(isLoggedIn(false))
%Argument of type 'CanActivateFn' is not assignable to parameter of type '() => boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree>'. Target signature provides too few arguments. Expected 2 or more, but got 0. -
TestBed.runInInjectionContext(isLoggedIn(false) as any)
个➔Can't resolve all parameters for isLoggedIn: (?).正在试运行 -
TestBed.runInInjectionContext(isLoggedIn(false)(activatedRouteSnapshot, null))
%Argument of type 'boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree>' is not assignable to parameter of type '() => unknown'. Type 'boolean' is not assignable to type '() => unknown'. -
TestBed.runInInjectionContext(isLoggedIn(false)(activatedRouteSnapshot, null) as any);
%inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with 101. -
TestBed.runInInjectionContext(isLoggedIn(false) as any)(activatedRouteSnapshot, null);
%This expression is not callable. Type '{}' has no call signatures. -
(TestBed.runInInjectionContext(isLoggedIn(false) as any) as any)(activatedRouteSnapshot, null)
%Can't resolve all parameters for isLoggedIn: (?).
我还try 省略工厂函数,并使用Routing Data作为参数,如图here所示,但注入ActivatedRouteSnapshot
是不可能的,使用ActivatedRoute
会产生嵌套的可观察对象……一定有一种更简单的方法.
How can I test the functional router guard?个