我试图执行简单的路由单元测试,但是
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无法获得实现。有什么想法吗?
老实说,我在我的另一个简单的项目中也有同样的实现,它在那里工作得很好,但是在这里……