I try to test a routerlink click that should route from /parent to /parent/child in the root router-outlet. When i start my application everything works, but in my test i get this error message:
Error: Cannot match any routes. URL Segment: 'child'
My routes:
export const routes: Routes = [
{
path: 'parent',
pathMatch: 'full',
component: ParentComponent
},
{
path: 'parent/child',
component: ChildComponent
}
];
HTML on parent (/parent) that should route to /parent/child
<a routerLink="./child">child</a>
this works aswell but not in test:
<a routerLink="child">child</a>
My test:
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");
}));
});
The route itself is there, because when i try something like this, it works:
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");
}));
It seems that my test cant handle the routerLink directly.