代码之家  ›  专栏  ›  技术社区  ›  Mateusz Gebroski

测试床类型错误:无法读取未定义的属性“navigate”

  •  0
  • Mateusz Gebroski  · 技术社区  · 6 年前

    我试图执行简单的路由单元测试,但是

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   Thor Jacobsen    6 年前

    既然如此 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');
        }));
    })