ウェブサイト検索

typescript - Angular ユニットテストで、redirectTo に定義されたルートではなくルートで止まってしまう


ディレクティブの単体テストを試みていますが、ルーティングの設定に関して奇妙な問題が発生しています。path: '' に対して foo へのリダイレクトを設定しているにもかかわらず、テストケース内でログを取るとルートはまだ / になっています。

だから私の質問は、ログに記録するときになぜルートが 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''} を試してみましたが、それでも / が表示されます。