angular - Angular 测试 Jasmine 和 Karma 设置 router.routerState.snapshot.url
问题描述
我有 Anagular 应用程序版本 8,测试用 Karma 和 Jasmine 编写。
我有服务:
[...]
constructor(private router: Router) {}
public setParamByUrl(): Home {
const splitUrl = this.router.routerState.snapshot.url.split('/');
[...]
return {
codePostal: splitUrl[0],
[...]
}
}
分析this.router.routerState.snapshot.url
但我无法设置this.router.routerState.snapshot.url
Jasmine 和 Karma 文件:
describe('Routing Home', () => {
let routingService: RoutingService;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
NgbModule,
DfModule,
NgSelectModule,
NgOptionHighlightModule,
FormsModule,
[...]
],
providers:
[
{
provide: ActivatedRoute,
useValue: {
snapshot: {
url: 'http://localhost:8080/#/home/1X/fox?tango=MOON'
}
}
}
],
declarations: [],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
routingService = TestBed.get(RoutingHome);
});
it('Test url', () => {
expect(routingService.setAndGetScope()).toEqual({
codePostal: '123',
[...]
});
});
});
换句话说,我希望在测试文件中设置 URL,以便测试接收在 beforeEach 部分设置的 url。
我尝试过:
provide: ActivatedRoute,
useValue: {
snapshot: {
url: 'http://localhost:8080/#/home/1X/fox?tango=MOON'
}
但它不起作用。
解决方案
推荐阅读
- reactjs - 避免详尽深度警告的 useEffect 方法
- java - 尝试通过 Firebase 接收推送通知时,Android 应用程序崩溃
- reactjs - 在刷新中断时反应路由器
- php - 基于键的多数组div
- anylogic - 有没有办法返回代理与其目的地之间的网络中的节点列表?
- reactjs - 使用 React 以不同的角色和验证登录
- c - 如果我将代码添加到(不相关的)打印部分,“用于测量执行周期的双线程计数器模型”会更改输出
- c# - 有没有办法使用 WPF 的箭头键移动图片?
- angular - 观察反应角度形式的原始状态时的问题
- django - 显示可能不存在的对象的页面?