我做了一个正常运行的简单app
。
现在我正在尝试编写该应用程序的测试用例,因此我尝试使用routing
。
我的路由代码是
主要模块:
export const routes: Routes = [
{ path: '', redirectTo: '/users', pathMatch: 'full' },
];
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
UserModule,
HttpClientModule,
RouterModule.forRoot(routes)
],
功能模块:
const routes: Routes = [
{path: 'users', component: ListingComponent}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(routes)
],
declarations: [ListingComponent]
})
代码
我尝试运行我的spec
,但我遇到了错误
describe('Initial navigation', () => {
it('default route redirects to home (async)', fakeAsync(() => {
router.initialNavigation(); // triggers default
fixture.detectChanges();
tick();
console.log('==================');
console.log(location.path());
// fixture.whenStable().then(() => {
expect(location.path()).toBe('/users');
// })
}));
});
答案 0 :(得分:0)
如果您将UserModule
导入规范,则可解决错误。当AppModule
模块导入UserModule
以注册用户特征/模块路由时,还必须在规范中导入它以确保其规范中的路由注册也可用。
文档Testing: Import a feature module中的基本级别隐含了对此的需求。
//setup
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
UserModule, // import module
RouterTestingModule.withRoutes(routes)],
declarations: [
TestComponent,
]
});
fixture = TestBed.createComponent(TestComponent);
router = TestBed.get(Router);
location = TestBed.get(Location);
});
这是一个更新的StackBlitz演示功能(测试通过时没有错误)。