TestBed TypeError: Cannot read property 'navigate' of undefined
TestBed TypeError: Cannot read property 'navigate' of undefined
我正在尝试执行简单的路由单元测试,但是
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 无法实现。有任何想法吗?
老实说,我在我的另一个简单项目中有相同的实现,在那里工作正常但在这里..
鉴于 compileComponents
是一个异步进程,then
部分中的所有内容都是 运行 在 您的测试 运行.
尝试重构为:
fdescribe('AppRoutingModule', () => {
let location: Location;
let router: Router;
let fixture;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes(routes),
WelcomeModule
],
declarations: [
AppComponent,
],
providers: [
Location
]
}).compileComponents();
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');
}));
})
我正在尝试执行简单的路由单元测试,但是
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 无法实现。有任何想法吗? 老实说,我在我的另一个简单项目中有相同的实现,在那里工作正常但在这里..
鉴于 compileComponents
是一个异步进程,then
部分中的所有内容都是 运行 在 您的测试 运行.
尝试重构为:
fdescribe('AppRoutingModule', () => {
let location: Location;
let router: Router;
let fixture;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes(routes),
WelcomeModule
],
declarations: [
AppComponent,
],
providers: [
Location
]
}).compileComponents();
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');
}));
})