角度测试路由器params打破测试床

Joh*_*hnC 29 angular2-routing angular

当我提供params到我TestComponent的测试床时,如果html包含一个[routerLink]

试验台设置

TestBed.configureTestingModule({
        imports: [SharedModule.forRoot(), ManagementModule, HttpModule, RouterModule.forRoot([{
            path: '', component: TestComponent
        }])],
        declarations: [TestComponent],
        providers: [
            BaseRequestOptions,
            MockBackend,
            {
                provide: Http, useFactory: function (backend: MockBackend, defaultOptions: BaseRequestOptions) {
                    return new Http(backend, defaultOptions);
                },
                deps: [MockBackend, BaseRequestOptions]
            },
            { provide: APP_BASE_HREF, useValue: '/' },
            {
                provide: ActivatedRoute,
                useValue: {
                    params: Observable.of({ versionId: '1' }),
                    parent: {
                        params: Observable.of({ uniqueId: '1234' })
                    }
                }
            }
        ]
    });
    TestBed.compileComponents();
});
Run Code Online (Sandbox Code Playgroud)

记录错误

Failed: Error in ./DetailComponent class DetailComponent - inline template:72:20 caused by: Cannot read property '_lastPathIndex' of undefined
TypeError: Cannot read property '_lastPathIndex' of undefined
    at findStartingPosition (webpack:///home/developer/Projects/project/~/@angular/router/src/create_url_tree.js:184:0 <- src/test.ts:100429:23)
    at createUrlTree (webpack:///home/developer/Projects/project/~/@angular/router/src/create_url_tree.js:27:21 <- src/test.ts:100272:45)
    at Router.createUrlTree (webpack:///home/developer/Projects/project/~/@angular/router/src/router.js:424:0 <- src/test.ts:28688:111)
    at RouterLinkWithHref.get [as urlTree] (webpack:///home/developer/Projects/project/~/@angular/router/src/directives/router_link.js:253:0 <- src/test.ts:50778:32)
    at RouterLinkWithHref.updateTargetUrlAndHref (webpack:///home/developer/Projects/project/~/@angular/router/src/directives/router_link.js:246:0 <- src/test.ts:50771:91)
    at RouterLinkWithHref.ngOnChanges (webpack:///home/developer/Projects/project/~/@angular/router/src/directives/router_link.js:217:67 <- src/test.ts:50742:74)
    at Wrapper_RouterLinkWithHref.ngDoCheck (/RouterModule/RouterLinkWithHref/wrapper.ngfactory.js:101:18)
    at DebugAppView.View_DetailComponent3.detectChangesInternal (/ManagementModule/DetailComponent/component.ngfactory.js:548:33)
    at DebugAppView.AppView.detectChanges (webpack:///home/developer/Projects/project/~/@angular/core/src/linker/view.js:425:0 <- src/test.ts:95635:14)
    at DebugAppView.detectChanges (webpack:///home/developer/Projects/project/~/@angular/core/src/linker/view.js:620:0 <- src/test.ts:95830:44)
    at ViewContainer.detectChangesInNestedViews (webpack:///home/developer/Projects/project/~/@angular/core/src/linker/view_container.js:67:0 <- src/test.ts:95967:37)
    at DebugAppView.View_DetailComponent0.detectChangesInternal (/ManagementModule/DetailComponent/component.ngfactory.js:85:14)
    at DebugAppView.AppView.detectChanges (webpack:///home/developer/Projects/project/~/@angular/core/src/linker/view.js:425:0 <- src/test.ts:95635:14)
    at DebugAppView.detectChanges (webpack:///home/developer/Projects/project/~/@angular/core/src/linker/view.js:620:0 <- src/test.ts:95830:44)
Run Code Online (Sandbox Code Playgroud)

模板线72

<a class="button" [routerLink]="['/']">Back</a>
Run Code Online (Sandbox Code Playgroud)

在一个理想的世界里,我想继续提供参数,任何想法为什么它会爆炸?

Jas*_*ett 27

我想出了问题/解决方案.

我们看到Cannot read property '_lastPathIndex' of undefined因为Angular路由器在某一点_lastPathIndex上需要snapshot对象的属性.

Angular源代码的相关部分如下所示:

if (route.snapshot._lastPathIndex === -1) {
  return new Position(route.snapshot._urlSegment, true, 0);
}
Run Code Online (Sandbox Code Playgroud)

如果snapshot未定义,它当然会引起我们看到的错误.

这个问题可以通过制作snapshot非未定义来解决.

这就是我做到的.我的代码与OP有点不同.

我有一个叫做MockActivatedRoute类似的课程.

export class MockActivatedRoute {
  parent: any;
  params: any;

  constructor(options) {
    this.parent = options.parent;
    this.params = options.params;
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我在测试中使用它的方式.

let mockActivatedRoute = new MockActivatedRoute({
  parent: new MockActivatedRoute({
    params: Observable.of({ id: '1' })
  })
});
Run Code Online (Sandbox Code Playgroud)

为了使错误消失,我刚刚添加了一个snapshot属性MockActivatedRoute.

export class MockActivatedRoute {
  parent: any;
  params: any;
  snapshot = {};

  constructor(options) {
    this.parent = options.parent;
    this.params = options.params;
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 你可以添加类似于提供者的东西:providers:[{provide:ActivatedRoute,useValue:{'params':Observable.from([{'id':1}]),'snapshot':{}}},] (9认同)