我正在尝试执行简单的路由单元测试,但
TypeError: Cannot read property 'navigate' of undefined
被抛出。
我正在遵循这样的教程
https://codecraft.tv/courses/angular/unit-testing/routing/
这是我的实现方式
路线:
export const routes: Routes = [
{path: '', pathMatch: 'full', redirectTo: 'welcome'},
{path: 'welcome', component: WelcomeComponent},
{path: 'offer', loadChildren: () => import('./customer-dashboard/customer-dashboard.module').then(mod => mod.CustomerDashboardModule)},
{path: 'underConstruction', component: UnderDevelopmentComponent}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
和单元测试:
fdescribe('AppRoutingModule', () => {
let location: Location;
let router: Router;
let fixture;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes(routes),
WelcomeModule
],
declarations: [
AppComponent,
],
providers: [
Location
]
}).compileComponents()
.then(() => {
router = TestBed.get(Router);
location = TestBed.get(Location);
fixture = TestBed.createComponent(AppComponent);
router.initialNavigation();
})
});
it('should navigate from empty path to welcome', fakeAsync(() => {
router.navigate(['']);
tick();
expect(location.path()).toBe('/welcome');
}))
})
这里没什么好看的,这只是基本设置。看起来TestBed无法获得实现。有任何想法吗? 老实说,我在其他简单项目中也有相同的实现,并且在这里也可以正常工作。
答案 0 :(得分:1)
假设openssl rand -out my.key 128
是一个异步过程,则compileComponents
部分中的所有内容都会在测试运行后 运行。
尝试对此进行重构:
then