Angular Testbed测试路线找不到模块

时间:2019-04-23 15:59:15

标签: angular testbed

我正在尝试使用TestBed测试简单的重定向,但仍然看到以下错误:

  

Error: Uncaught (in promise): Error: Cannot find module ./pages/login/login.module#LoginPageModule

我的测试如下:

describe('AppComponent', () => {

  let statusBarSpy, splashScreenSpy, platformReadySpy, platformSpy, platformIsSpy, storageSpy;
  let router: Router;
  let location: Location;
  let fixture, loginFixture;
  let comp: AppComponent;

  beforeEach(async(() => {
    statusBarSpy = jasmine.createSpyObj('StatusBar', ['styleDefault']);
    splashScreenSpy = jasmine.createSpyObj('SplashScreen', ['hide']);
    platformReadySpy = Promise.resolve();
    platformIsSpy = Promise.resolve('ipad');
    platformSpy = jasmine.createSpyObj('Platform', { ready: platformReadySpy, is: platformIsSpy })
    storageSpy = jasmine.createSpyObj('Storage', ['get', 'set', 'clear', 'remove', 'ready']);

    TestBed.configureTestingModule({
      declarations: [AppComponent],
      imports: [

        IonicStorageModule.forRoot(),
        HttpClientTestingModule,
        LoginPageModule,
        RouterTestingModule.withRoutes(routes),

      ],
      schemas: [CUSTOM_ELEMENTS_SCHEMA],
      providers: [
        { provide: Storage, useClass: StorageMock },
        { provide: StatusBar, useValue: statusBarSpy },
        { provide: SplashScreen, useValue: splashScreenSpy },
        { provide: Platform, useValue: platformSpy },
      ],
    }).compileComponents();

    router = TestBed.get(Router);
    location = TestBed.get(Location);
    fixture = TestBed.createComponent(AppComponent);
    loginFixture = TestBed.createComponent
    comp = fixture.componentInstance;
    router.initialNavigation();
  }));

  it('navigates to "" redirects you to /login', fakeAsync(() => {
    router.navigate(['/login']);
    tick();
    expect(location.path()).toBe('/login')
  }));
});

我在 app-routing.module.ts 中的路线是:

export const routes: Routes = [
  {
    path: '',
    redirectTo: 'login',
    pathMatch: 'full'
  },
  {
    path: 'login', 
    loadChildren: './pages/login/login.module#LoginPageModule'
    // component: LoginPage
  },
  {
    path: 'manager-dashboard',
    canActivate: [AuthGuard],
    loadChildren: './pages/manager-dashboard/manager-dashboard.module#ManagerDashboardPageModule'
  },
];

我的 LoginPageModule 是:

const routes: Routes = [
  {
    path: '',
    component: LoginPage
  }
];

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    RouterModule.forChild(routes)
  ],
  declarations: [LoginPage]
})
export class LoginPageModule {}

要完成这项工作,我需要做些什么吗?

1 个答案:

答案 0 :(得分:1)

您需要在测试时将延迟加载的模块存根。在beforeEach函数中添加以下行:-

const loader = TestBed.get(NgModuleFactoryLoader);

然后在您的测试用例中,在导航代码之前,添加以下内容:-

loader.stubbedModules = {lazyModule: LoginPageModule};

例如:

@Component({ template: '' })
class LazyLoadedComponent { }

@NgModule({ declarations: [ LazyLoadedComponent ]})
class LazyModule { }

it('navigates to "" redirects you to /login', fakeAsync(() => {
    const loader = TestBed.get(NgModuleFactoryLoader);

    loader.stubbedModules = {
       './pages/login/login.module#LoginPageModule': LazyModule,
    };

    router.navigate(['/login']);
    tick();
    expect(location.path()).toBe('/login')
}));

有关更多详细信息,您可以在此处阅读有关测试延迟加载的模块的信息:- https://angular.io/api/router/testing/SpyNgModuleFactoryLoader https://github.com/angular/angular/issues/17902

希望对您有帮助。