angular 2.0.0-rc.1 + karma: provide RouteSegment

懵懂的女人 提交于 2019-12-05 18:30:34

I had the same issue, I had to look at the angular router code and the specs they have in there repo. You should be able to provide the routeSegment like this:

import {RouteSegment, Router} from '@angular/router';
provide(RouteSegment, {useFactory: (r: any) => r.routeTree.root, deps: [Router]})

or you can just mock out a new RouteSegment similar to how you did with RouteParams:

provide(RouteParams, { useValue: new RouteParams({ page: 'terms-conditions' }) }),

Is now this:

provide(RouteSegment, { useValue: new RouteSegment([], { page: 'terms-conditions' }, null, null, null) }),

To create it you just have to pass it a couple of extra parameters.

Steve Van Opstal

Solution 1

See the answer of HomeBrew

Didn't work for me, still not sure why.

Solution 2

A mock implementation of RouteSegment:

class MockRouteSegment implements RouteSegment {
  urlSegments: any;
  parameters: any;
  outlet: string;
  _type: any;
  _componentFactory: any;
  type: any;
  stringifiedUrlSegments: string;
  constructor(parameters?: { [key: string]: any; }) {
    this.parameters = parameters
  }
  getParam(param: string) {
    return this.parameters[param];
  }
}

Provide it like this:

provide(RouteSegment, { useValue: new MockRouteSegment({ 'key': 'value' }) })
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!