简体   繁体   English

Angular:使用路由对延迟加载的模块进行单元测试

[英]Angular: Unit testing lazy-loaded modules with routing

I'm facing some issues testing lazy-loaded modules in Angular. 我在Angular中测试延迟加载的模块时遇到一些问题。 This is my .spec file: 这是我的.spec文件:

  import { Location } from '@angular/common';
    import { TestBed, ComponentFixture } from '@angular/core/testing';
    import { RouterTestingModule } from '@angular/router/testing';
    import { Router } from '@angular/router';
    import { routes } from './app-routing.module';
    import { AppComponent } from './app.component';
    import { AppConfigService } from './services/appConfig.service';
    import { TranslateModule } from '@ngx-translate/core';
    import { NgModuleFactoryLoader } from '@angular/core';
    import { VehicleModule} from './views/vehicle/vehicle.module';
    import { DriverModule} from './views/driver/driver.module';
    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

    describe('Router: App', () => {

      let location: Location;
      let router: Router;
      let fixture: ComponentFixture<AppComponent>;
      let loader: any;

      beforeEach(() => {
        TestBed.configureTestingModule({
          imports: [
            BrowserAnimationsModule,
            TranslateModule.forRoot(),
            RouterTestingModule.withRoutes(routes),
          ],
          declarations: [AppComponent],
          providers: [AppConfigService]
        });

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

        loader = TestBed.get(NgModuleFactoryLoader);
        loader.stubbedModules = {
          'VehicleModule': VehicleModule,
          'DriverModule': DriverModule
        };

        fixture = TestBed.createComponent(AppComponent);

        router.resetConfig([
          {
            path: 'vehicle',
            loadChildren: 'VehicleModule'
          },
          {
            path: 'driver',
            loadChildren: 'DriverModule'
          },
          {
            path: '',
            loadChildren: 'VehicleModule'
          }
        ]);

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

      it('should create APP', () => {
        expect(fixture.componentInstance).toBeDefined();
      });

      it('lazily navigates to "/driver"',(() => {
        router.navigate(['/driver']);
        expect(location.path()).toBe('/driver');
      }));
    });

This is what i got from running the test: 这是我从运行测试中得到的:

 Expected '' to be '/driver'.

The routes work fine on the app, the problem is showing up only during the unit testing session. 路由在应用程序上工作正常,问题仅在单元测试期间显示。

What am i missing? 我想念什么?

I'm using karma 1.7.1, Angular 6, jasmine 2.99.1 我正在使用业力1.7.1,Angular 6,茉莉2.99.1

Thanks. 谢谢。

Maybe it is a bit outdated, but here is the code of my lazy loaded module test. 也许有些过时了,但这是我的延迟加载模块测试的代码。

The path to the module is: 该模块的路径为:

{path: 'student', loadChildren: './student/student.module#StudentModule', canLoad: [AuthGuard]} {路径:“ student”,loadChildren:“ ./ student / student.module#StudentModule”,canLoad:[AuthGuard]}

it('should navigate to /student lazy loaded ,pdule', fakeAsync(() => {

        const loader = TestBed.get(NgModuleFactoryLoader);
        loader.stubbedModules = {lazyModule: StudentModule};

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

        router.navigate(['student'])

        tick();

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

When I will remove: 当我删除时:

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

an exception will be thrown: Expected '' to be '/student'. 将会抛出异常: 期望''为'/ student'。

I am also attaching TestBed configuration: 我还附加了TestBed配置:

    let location: Location;
        let router: Router;
        let fixture;
          beforeEach(()=> {
            TestBed.configureTestingModule({
                imports: [
                    BrowserAnimationsModule,
                    RouterTestingModule.withRoutes(routes),
                    SharedModule,
                    AuthModule,
                    MaterialModule,
                    StoreModule.forRoot(reducers),
                    EffectsModule.forRoot([AuthEffects, UserEffects])
                ],
                declarations: [
                    AppComponent,
                    WelcomeComponent,
                    NavbarComponent,
                    ProjectInfoComponent
                ],
                providers: [
                    Location
                ]
            }).compileComponents();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 角度5:如何路由嵌套的延迟加载模块? - Angular 5: How to routing nested lazy-loaded modules? Angular 7子路由延迟加载模块不起作用 - Angular 7 sub-routing lazy-loaded modules are not working 单元测试延迟加载模块路由 - Unit testing lazy loaded modules routing 安全地保护延迟加载的模块(Angular 2) - Securely Guarding Lazy-Loaded Modules (Angular 2) 具有多个命名出口的延迟加载模块上的Angular2路由 - Angular2 Routing on lazy-loaded module with multiple named outlets 角度的 <custom-component> 在延迟加载的模块上不起作用 - Angular <custom-component> doesnt work on lazy-loaded modules 将 Angular 从 8 更新到 9 后延迟加载的模块警告 - Lazy-loaded modules warnings after updating Angular from 8 to 9 延迟加载的 Angular 模块无法使用@nguniversal 渲染服务器端,而客户端路由和渲染工作 - Lazy-loaded Angular modules don't get server side rendered with @nguniversal, while client side routing and rendering works 来自延迟加载模块的Angular 2延迟加载模块 - Angular 2 lazy-loading modules from within a lazy-loaded module 角,在延迟加载的模块中暴露组件 - Angular, exposing components in lazy-loaded module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM