3

我正在尝试对指令进行单元测试,但我在路由设置方面遇到了一个奇怪的问题。尽管路由重定向到仍然是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'}但它仍然给出/

4

0 回答 0