角度测试SpyNgModuleFactoryLoader.stubbedModules无法识别2个惰性模块

时间:2018-05-25 14:39:54

标签: angular karma-jasmine angular6 angular-testing

我正在尝试使用具有角度6业力的懒惰模块来测试路由器导航。但我得到了控制台错误

Cannot find module app/features/landing/landing.module#landingPageModule

然后我用

loader = TestBed.get(NgModuleFactoryLoader); loader.stubbedModules = { 'landingPageModule': LandingPageModule };

 router.resetConfig([
    {path: 'main', loadChildren: 'landingPageModule'},
    {path: 'main/auth/login', loadChildren: 'AuthModule'},
    {path: 'main/auth/sign-up', loadChildren: 'AuthModule'}
]);

导航时一切都很好

{path: 'main', loadChildren: 'landingPageModule'},

但是当我在航行时

{path: 'main/auth/login', loadChildren: 'AuthModule'},

我还在

控制台上出现

Cannot find module app/features/auth/auth.module#AuthModule错误。

LandingPageroutes看起来很喜欢这个

export const landingPageRoutes: Routes = [
{path: '', component: HomeComponent, canActivate: [AuthGuard],data: {roles: ['user']}},
{
    path: 'auth', component: LandingPageComponent, children: [
        {path: '', pathMatch: 'full', redirectTo: 'login'},
        {path: '', loadChildren: 'app/features/auth/auth.module#AuthModule'},
    ]
},

AuthModule在LandingPageModule上声明,所以我需要在" SpyNgModuleFactoryLoader"上存储2个模块。但它不起作用

loader = TestBed.get(NgModuleFactoryLoader);
    loader.stubbedModules = {
        'AuthModule' : AuthModule,
        'landingPageModule': LandingPageModule
    };

1 个答案:

答案 0 :(得分:0)

问题在于

 router.resetConfig([
        {path: 'main', loadChildren: 'landingPageModule'},
        {path: 'main/auth/login', loadChildren: 'AuthModule'},
        {path: 'main/auth/sign-up', loadChildren: 'AuthModule'}
    ]);

无需在router.resetConfig上声明2个不同的模块 只需将更改AuthModule替换为landingPageModule

     router.resetConfig([
        {path: 'main', loadChildren: 'AuthModule'},
        {path: 'main/auth/login', loadChildren: 'AuthModule'},
        {path: 'main/auth/sign-up', loadChildren: 'AuthModule'}
    ]);