我正在使用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]
我们如何测试这些路线?
答案 0 :(得分:20)
好的,我刚刚发现了。我应该更多地关注错误日志。我会回答自己,以防它帮助某人。
设置存根模块这有效:
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');
}));