角单元测试路由卡在根上,而不是在redirectTo中定义的路由

时间:2019-06-03 10:25:09

标签: angular typescript angular-routing angular-unit-test

我正在尝试对指令进行单元测试,但是在路由设置方面遇到了一个奇怪的问题。尽管我将foo重定向到path: '',但当我在测试用例中记录路由时,该路由仍为/

所以我的问题是为什么登录时该路由为什么不foo

describe('IsRouteDirective', () => {

  let directive: IsRouteDirective;
  let hostElement: DebugElement;

  beforeEach(() => {

    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule.withRoutes([
          {
            path: '',
            pathMatch: 'full',
            redirectTo: 'foo'
          },
          {
            path: 'foo',
            component: LibRouteComponent
          },
          {
            path: 'bar',
            component: LibRouteComponent
          }
        ])
      ],
      declarations: [
        LibTestComponent,
        LibRouteComponent,
        IsRouteDirective
      ]
    });
  });

  it('should have a class called \'is-route\'', async(() => {

    // helper function that calls TestBed.overrideComponent, TestBed.compileComponents, TestBed.createComponent and returns the ComponentFixture
    overrideAndCompileComponent(LibTestComponent, `
      <div [libIsRoute]="['foo']"></div>
      <router-outlet></router-outlet>
    `).then((fixture) => {
      hostElement = fixture.debugElement.query(By.directive(IsRouteDirective));
      directive = hostElement.injector.get(IsRouteDirective);
      fixture.detectChanges();

      const injector = getTestBed();
      const router = injector.get(Router);

      // logs '/'
      console.log(router.url);
    });
  }));
});

我也尝试过使用{path: '**', redirectTo: 'foo'},但它仍然给出/

0 个答案:

没有答案