Angular单元测试中如何设置当前路由?
创始人
2024-10-23 22:30:36
0
  1. 首先需要导入RouterTestingModule模块,并在beforeEach方法中使用TestBed.configureTestingModule方法来配置测试环境:
import { RouterTestingModule } from '@angular/router/testing';
...
beforeEach(() => {
  TestBed.configureTestingModule({
    imports: [RouterTestingModule],
    ...
  }).compileComponents();
});
  1. 接着,在测试用例中,使用RouterTestingModule.inject来注入Router服务,然后调用navigateByUrl方法来设置当前路由:
import { Router } from '@angular/router';
...
it('should navigate to /home', inject([Router], (router: Router) => {
  const navigateSpy = spyOn(router, 'navigateByUrl');
  component.goToHome();
  expect(navigateSpy).toHaveBeenCalledWith('/home');
}));

上面的示例中,我们在组件中定义了一个名为goToHome的方法,用于导航到/home路由。

这样通过设置RouterTestingModule模块并注入Router服务就可以在Angular单元测试中设置当前路由啦!

相关内容

热门资讯

透视辅助"aapok... 透视辅助"aapoker怎么选牌"详细辅助插件教程(私人局有透视攻略)关于aapoker怎么选牌机制...
透视实锤"wepok... 透视实锤"wepoker私人局辅助挂"详细辅助透牌教程(可以下载游戏攻略);大神普及一款德州ai内幕...
透视ai"poker... 透视ai"pokermaster脚本"详细辅助软件教程(私人局辅助挂辅助);实战中需综合运用上述技巧...
透视ai"德州局hh... 透视ai"德州局hhpoker"详细辅助靠谱教程(手机版透视脚本神器);《WPK辅助透视》‌:支持手...
透视能赢"wepok... 透视能赢"wepoker透视脚本免费app"详细辅助技巧教程(透视脚本免费透明);人气非常高,ai更...
透视辅助"aapok... 透视辅助"aapoker插件"详细辅助教你攻略(俱乐部辅助方法)1、玩家可以在aapoker插件软件...
透视ai"wepok... 这是一款非常优秀的wepoker俱乐部辅助 ia辅助检测软件,能够让你了解到wepoker俱乐部辅助...
透视实锤"WePOk... 这是一款非常优秀的WePOker有没有透视方法 ia辅助检测软件,能够让你了解到WePOker有没有...
透视辅助"wepok... 透视辅助"wepoker辅助分析器"详细辅助2025新版教程(可以设置盖牌软件)1、玩家可以在wep...
透视透视挂"wepo... 这是一款非常优秀的wepoker免费透视脚本 ia辅助检测软件,能够让你了解到wepoker免费透视...