angular - How to test guards relying on http request?
问题描述
I have a problem with my test and I have no idea how to resolve that:
I have two methods in my service:
isAuthenticated () {
const token = localStorage.getItem('token');
if (token) {
return this.verifyToken(token).pipe(
map(data => true),
catchError (error => of(false))
);
}
return of(false);
}
verifyToken (token: string): Observable<any> {
const data = {'token': token};
return this.http.post(url/verify/, data);
}
And canActivate method in my guard:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.auth.isAuthenticated().pipe(
map(data => {
if (data) {
this.router.navigate(['/url/']);
}
return !data;
})
);
}
So if user is authenticated he can't hit url (login page). And my test looks:
let guard: Guard;
let service: Service;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [CommonModule],
providers: [
Service,
Guard,
]
}).compileComponents();
guard = TestBed.get(Guard);
service = TestBed.get(Service);
});
it('can't see login page when he is logged in', fakeAsync(() =>{
spyOn(service, 'isAuthenticated').and.callFake(()=>{return true;});
guard.canActivate(new ActivatedRouteSnapshot(), mockSnapshot).subscribe(
x => expect(x).toBeFalsy()
);
}));
I got
TypeError: this.auth.isAuthenticated(...).pipe is not a function
how should I test it correctly?
解决方案
你的服务实现返回一个 Observable。你的间谍返回一个布尔值。
我会这样
it('can not see login page when he is logged in', () => {
spyOn(service, 'isAuthenticated').and.callFake(() => { return of(true); });
guard.canActivate(new ActivatedRouteSnapshot(), mockSnapshot).subscribe(
x => expect(x).toBeFalsy()
);
}));
如果将 Observable 转换为 Promise,则可以使用 Typescript 的 await / async 语法,而不是订阅 Observable。
推荐阅读
- javascript - 试图确定“event.target.contains===this.node”的含义
- expression - 相关表的按日期自定义函数
- laravel - 尝试获取非对象的属性“名称”时出错
- docker - 从 Docker Hub 存储库安装 Hydra 映像会出现错误:OCI 运行时创建失败:container_linux.go:346
- flutter - 在没有streambuilder的情况下获取firebase数据
- angular - 尝试使用具有不同模板的相同组件
- jenkins - Jenkins:使用外部脚本与在 Groovy 中编码工作
- java - JPA处理SQLServer的查询参数限制
- c# - Xamarin Forms - 带有模板按钮的操作栏/工具栏?
- reactjs - 重渲染动画滞后