单元测试延迟加载的模块路由

问题描述 投票:0回答:1

我在路由的单元测试中遇到了一些问题。我已经尝试过this解决方案,但是现在这种延迟加载表示法是deprecated,因此由于某种原因我的测试失败了。有人可以给我建议我的代码怎么了?

app-routing.module.ts

export const routes: Routes = [
{path: 'about', component: AboutComponent},
{path: 'files', loadChildren: () => import('./files/files.module').then(m => m.FilesModule)}, 
];

@NgModule({
 imports: [RouterModule.forRoot(routes)],
 exports: [RouterModule]
})
export class AppRoutingModule { }

app-routing.module.spec.ts

describe('AppRoutingModule', () => {
   let location: Location;
   let router: Router;
   let fixture;

   beforeEach(() => {
   TestBed.configureTestingModule({
       imports:[
           RouterTestingModule.withRoutes(routes),
           HttpClientModule,
           AboutComponent,
       ],
       declarations: [
           AppComponent,
           FilesComponent,
       ],
       providers: [{provide: APP_BASE_HREF, useValue: '/'}]
   })

   router = TestBed.get(Router);
   location = TestBed.get(Location);

   fixture = TestBed.createComponent(AppComponent);
   router.initialNavigation();
   });

   it('should navigate to files', fakeAsync(() => {
       const loader = TestBed.get(NgModuleFactoryLoader);
       loader.stubbedModules = {lazyModule : FilesModule }

       router.resetConfig([
           {path: 'files', loadChildren: 'lazyModule'},
       ]);

       router.navigate(['files']);
       tick(50);
       fixture.detectChanges();

       expect(location.path()).toBe('/files');
   }));
});

谢谢!

angular typescript unit-testing jasmine lazy-loading
1个回答
0
投票

您尝试过:

router.resetConfig([
  { path: 'files', loadChildren: () => Promise.resolve(lazyModule) },
 ]);

© www.soinside.com 2019 - 2024. All rights reserved.