Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度延迟加载模块,单元测试代码覆盖率_Angular_Unit Testing_Lazy Loading_Code Coverage_Karma Coverage - Fatal编程技术网

Angular 角度延迟加载模块,单元测试代码覆盖率

Angular 角度延迟加载模块,单元测试代码覆盖率,angular,unit-testing,lazy-loading,code-coverage,karma-coverage,Angular,Unit Testing,Lazy Loading,Code Coverage,Karma Coverage,如何为延迟加载模块编写单元测试用例 import { routes } from './app-routing'; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { } 路由:导出常数路由:路由=[ { 路径:“”, 重定向至“主页”, 路径匹配:“已满” }, { 路径:“家”, loadChildre

如何为延迟加载模块编写单元测试用例

import { routes } from './app-routing';
@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})
export class AppRoutingModule { }

路由:导出常数路由:路由=[ { 路径:“”, 重定向至“主页”, 路径匹配:“已满” }, { 路径:“家”, loadChildren:()=>import('./home/home.module')。然后(m=>m.HomeModule) }, { 路径:“qwe”, loadChildren:()=>import('./path2/qwe.module')。然后(m=>m.Qwemodule) }, { 路径:“abc”, loadChildren:()=>import('./path1/abc.module')。然后(m=>m.abc模块) } ];

等级库文件:

describe('Routes', () => {
  it(`should load 4 Routes`, async(() => {
      console.log(routes.length.toString());
      var a=routes.length;
      expect(a).toEqual(4);
  }));
});

describe('Routes load child', () => {
  let location: Location;
  let router: Router;
  beforeEach(() => {
    TestBed.configureTestingModule({
        imports:[
          RouterTestingModule.withRoutes(routes),
          RouterTestingModule,
            HttpClientModule,
           HomeModule,
            Qwemodule,
            Abcmodule
          ],
        providers: [{provide: APP_BASE_HREF, useValue: '/'}]
    });
  });

  it(`navigate to route  /path loades module`, fakeAsync(() => {
    router = TestBed.get(Router);
    routes.forEach(route => {
      location = TestBed.get(Location);
      router.navigate([route.path]);
      tick(50);
     let t= route.loadChildren;
     expect(route.loadChildren).not.toBeNull;
     expect(location.path()).toBe('/'+route.path);
    });
   }));
});


它在spc中隐藏路由器文件覆盖,但在代码覆盖率中未覆盖loadchild,因此它是20%如何实现100%的代码覆盖率

您应该为此使用NgModuleFactoryLoader和Location类

it('should navigate to lazy module', fakeAsync(() => {
    let router = TestBed.get(Router);
    router.initialNavigation();
    //Used to load ng module factories.
    let loader = TestBed.get(NgModuleFactoryLoader);
    let location = TestBed.get(Location);
    // sets up stubbedModules.
    loader.stubbedModules = {
      './path/module_name.module#Your_lazymodule': Your_lazymodule,
    };
    router.resetConfig([
      { path: 'instructor', loadChildren: './path/module_name.module#Your_lazymodule' },
    ]);
    router.navigateByUrl('/URL');
    tick();
    expect(location.path()).toBe('/URL');
  }));