Я пытаюсь проверить щелчок маршрутизатора, который должен перенаправляться из / parent в / parent / child в root router-outlet. Когда я запускаю свое приложение , все работает , но в моем тесте я получаю следующее сообщение об ошибке:
Ошибка: невозможно сопоставить никакие маршруты. Сегмент URL: 'child'
Мои маршруты:
export const routes: Routes = [
{
path: 'parent',
pathMatch: 'full',
component: ParentComponent
},
{
path: 'parent/child',
component: ChildComponent
}
];
HTML для родителя (/ parent), который должен направлять в / parent / child
<a routerLink="./child">child</a>
это работает также, но не в тесте:
<a routerLink="child">child</a>
Мой тест:
describe('ParentComponent', () => {
let component: ParentComponent;
let fixture: ComponentFixture<ParentComponent>;
let location: Location;
let router: Router;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes(routes),
AppModule
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ParentComponent);
component = fixture.componentInstance;
router = TestBed.get(Router);
location = TestBed.get(Location);
fixture.detectChanges();
});
it('router to child test', fakeAsync(() => {
router.navigate(["/parent"]);
tick();
expect(location.path()).toBe("/parent")
fixture.debugElement.query(By.css("a")).nativeElement.click();
tick();
expect(location.path()).toBe("/parent/child");
}));
});
Сам маршрут есть, потому что, когда я пытаюсь что-то подобное, он работает:
it('router to child test', fakeAsync(() => {
router.navigate(["/parent"]);
tick();
expect(location.path()).toBe("/parent")
//fixture.debugElement.query(By.css("a")).nativeElement.click();
router.navigate(["/parent/child"]);
tick();
expect(location.path()).toBe("/parent/child");
}));
Похоже, мой тест не может напрямую работать с routerLink.