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