加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用延迟加载模块路径进行角度测试

发布时间:2020-12-17 07:32:15 所属栏目:安全 来源:网络整理
导读:我正在使用RouterTestingModule,我的测试是 it('should navigate to foo child path',fakeAsync(() = { let router = TestBed.get(Router); let location = TestBed.get(Location); let fixture = TestBed.createComponent(AppComponent); router.initialNav
我正在使用RouterTestingModule,我的测试是
it('should navigate to foo child path',fakeAsync(() => {
  let router = TestBed.get(Router);
  let location = TestBed.get(Location);
  let fixture = TestBed.createComponent(AppComponent);
  router.initialNavigation();
  router.navigateByUrl('/foo/child');
  tick();
  fixture.detectChanges();
  expect(location.path()).toBe('/foo/child');
}));

当试图测试具有延迟加载的孩子的路线时,例如

{ path: 'foo',loadChildren: 'app/foo/foo.module#FooModule'}

抛出错误“无法找到模块…”.

Error: Uncaught (in promise): Error: Cannot find module app/foo/foo.module#FooModule
    Error: Cannot find module app/foo/foo.module#FooModule
        at SpyNgModuleFactoryLoader.Array.concat.SpyNgModuleFactoryLoader.load (webpack:///~/@angular/router/@angular/router/testing.es5.js:78:0 <- src/test.ts:92347:35) [ProxyZone]
        at RouterConfigLoader.Array.concat.RouterConfigLoader.loadModuleFactory (webpack:///~/@angular/router/@angular/router.es5.js:3417:0 <- src/test.ts:24005:129) [ProxyZone]
        at RouterConfigLoader.Array.concat.RouterConfigLoader.load (webpack:///~/@angular/router/@angular/router.es5.js:3401:25 <- src/test.ts:23989:52) [ProxyZone]
        at MergeMapSubscriber.project (webpack:///~/@angular/router/@angular/router.es5.js:1569:0 <- src/test.ts:22157:108) [ProxyZone]
        at MergeMapSubscriber.Array.concat.MergeMapSubscriber._tryNext (webpack:///~/rxjs/operator/mergeMap.js:120:0 <- src/test.ts:57460:27) [ProxyZone]
        at MergeMapSubscriber.Array.concat.MergeMapSubscriber._next (webpack:///~/rxjs/operator/mergeMap.js:110:0 <- src/test.ts:57450:18) [ProxyZone]
        at MergeMapSubscriber.Array.concat.Subscriber.next (webpack:///~/rxjs/Subscriber.js:89:0 <- src/test.ts:19369:18) [ProxyZone]
        at ScalarObservable.Array.concat.ScalarObservable._subscribe (webpack:///~/rxjs/observable/ScalarObservable.js:49:0 <- src/test.ts:56878:24) [ProxyZone]
        at ScalarObservable.Array.concat.Observable._trySubscribe (webpack:///~/rxjs/Observable.js:57:0 <- src/test.ts:232:25) [ProxyZone]
        at ScalarObservable.Array.concat.Observable.subscribe (webpack:///~/rxjs/Observable.js:45:0 <- src/test.ts:220:27) [ProxyZone]
        at MergeMapOperator.Array.concat.MergeMapOperator.call (webpack:///~/rxjs/operator/mergeMap.js:85:0 <- src/test.ts:57425:23) [ProxyZone]
        at Observable.Array.concat.Observable.subscribe (webpack:///~/rxjs/Observable.js:42:0 <- src/test.ts:217:22) [ProxyZone]
        at MergeMapOperator.Array.concat.MergeMapOperator.call (webpack:///~/rxjs/operator/mergeMap.js:85:0 <- src/test.ts:57425:23) [ProxyZone]
        at Observable.Array.concat.Observable.subscribe (webpack:///~/rxjs/Observable.js:42:0 <- src/test.ts:217:22) [ProxyZone]
        at resolvePromise (webpack:///~/zone.js/dist/zone.js:683:0 <- src/polyfills.ts:3331:17) [ProxyZone]
        at webpack:///~/zone.js/dist/zone.js:760:0 <- src/polyfills.ts:3408:17 [ProxyZone]
        at ProxyZoneSpec.Array.concat.ProxyZoneSpec.onInvokeTask (webpack:///~/zone.js/dist/proxy.js:103:0 <- src/test.ts:90529:39) [ProxyZone]
        at FakeAsyncTestZoneSpec.Array.concat.FakeAsyncTestZoneSpec.flushMicrotasks (webpack:///~/zone.js/dist/fake-async-test.js:204:0 <- src/test.ts:90058:17) [ProxyZone]
        at FakeAsyncTestZoneSpec.Array.concat.FakeAsyncTestZoneSpec.tick (webpack:///~/zone.js/dist/fake-async-test.js:187:0 <- src/test.ts:90041:18) [ProxyZone]
        at Object.tick (webpack:///~/@angular/core/@angular/core/testing.es5.js:389:0 <- src/test.ts:53426:29) [ProxyZone]
        at Object.advance (webpack:///src/app/test/test.module.ts:33:2 <- src/test.ts:85821:15) [ProxyZone]
        at Object.<anonymous> (webpack:///src/app/app.component.spec.ts:79:6 <- src/test.ts:85712:27) [ProxyZone]
        at Object.<anonymous> (webpack:///~/@angular/core/@angular/core/testing.es5.js:348:0 <- src/test.ts:53385:26) [ProxyZone]

我们如何测试这样的路线?

好的,我刚刚发现了.我应该更多地关注错误日志.我会回答自己,以防它帮助某人.

可以使用SpyNgModuleFactoryLoader设置存根模块

这有效:

it('should navigate to foo child path',fakeAsync(() => {
  let router = TestBed.get(Router);
  let location = TestBed.get(Location);
  let fixture = TestBed.createComponent(AppComponent);
  router.initialNavigation();

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

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

  router.navigateByUrl('/foo/child');

  tick();
  fixture.detectChanges();

  expect(location.path()).toBe('/foo/child');
}));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读