javascript - Angular-yksikkötestaus: Virhe: Ei voida löytää vastaavia reittejä. URL-segmentti: 'home/advisor'
Työstän yksikkötestausta Angular 4.0.0 -sovellukseni alla, ja jotkin metodit todellisessa komponentissani kutsuvat manuaalista reititystä seuraavasti:
method(){
....
this.navigateTo('/home/advisor');
....
}
metodilla navigateTo on mukautettu reititystapa, joka kutsuu tätä:
public navigateTo(url: string) {
this.oldUrl = this.router.url;
this.router.navigate([url], { skipLocationChange: true });
}
minulla on tämä reititystiedosto :
import ... // Components and dependencies
const homeRoutes: Routes = [
{
path: 'home', component: HomeComponent,
children: [
{
path: 'registration',
component: RegistrationComponent,
children: [
{
path: 'synthese',
component: SyntheseComponent
},
{
path: 'queue',
component: QueueComponent,
children: [
{
path: 'queue-modal',
component: QueueModalComponent
},
{
path: 'confirm',
component: ConfirmComponent
}
]
}
]
},
{
path: 'toolbox',
component: ToolboxComponent
},
{
path: 'appointment',
component: AppointmentRegistrationComponent
},
{
path: 'appointment-validation',
component: AppointmentValidationComponent
},
{
path: 'datepicker',
component: DatePickerComponent
},
{
path: 'validation/:defaultNumber',
component: ValidationComponent,
children: [
{
path: 'synthese',
component: SyntheseComponent
}
]
},
{
path: 'modalField',
component: ModalFieldComponent
},
{
path: 'search',
component: SearchComponent
},
{
path: 'advanced-search',
component: AdvancedSearchComponent
},
{
path: 'tools',
component: ToolsComponent
},
{
path: 'advisor',
component: AdvisorComponent
},
{
path: 'pilote',
component: PilotComponent
},
{
path: 'blank',
component: BlankComponent
},
{
path: 'view-360/:id',
component: View360Component,
children: [
{
path: 'client',
component: ClientComponent
},
{
path: 'tools',
component: ToolsAdvisorComponent
},
{
path: 'valid-close',
component: ValidCloseComponent
},
{
path: 'application',
component: ApplicationView360Component
}
],
canActivate: [AuthGuardAdviser]
},
{
path: 'view-360',
component: View360Component,
children: [
{
path: 'client',
component: ClientComponent
}
],
canActivate: [AuthGuardAdviser]
},
{
path: 'contract',
component: ContractComponent
},
{
path: 'queue-again',
component: QueueAgainComponent
},
{
path: 'stock',
component: StockComponent,
children: [
{
path: 'mobile',
component: MobileComponent
},
{
path: 'stock-level',
component: StockLevelComponent
}
]
},
{
path: 'usefull-number',
component: UsefullNumberComponent
},
{
path: 'admin',
loadChildren: 'app/home/admin/admin.module#AdminModule',
// component: AdminComponent,
canActivate: [AuthGuardAdmin]
},
{
path: 'rb',
loadChildren: 'app/home/rb/rb.module#RbModule',
// component: RbComponent
// canActivate: [AuthGuardAdmin]
},
{
path: 'tools-advisor',
component: ToolsAdvisorComponent
},
{
path: 'catalog/:haveClient',
component: CatalogComponent
},
{
path: 'application',
component: ApplicationComponent
},
]
},
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(homeRoutes)
],
exports: [
RouterModule
],
declarations: []
})
export class HomeRoutingModule { }
Omituisesti, jopa minun sovellukseni toimii käytännöllisesti hyvin, mutta testi heittää tämän virheen:
Epäonnistui: Kaappaamaton (lupauksessa): Virhe: Yhtään reittiä ei löydy. URL-osio: 'home/advisor' Virhe: Yhtään reittiä ei löydy. URL-osio: 'home/advisor'
Näyttää siltä, että minulta puuttuu joitakin asetuksia.
Onko mitään ideoita??
Vastaus #1
Tarvitset RouterTestingModule.withRoutes näin:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes(
[{path: 'yourpath', component: BlankComponent}]
)
],
declarations: [
BlankComponent,
YourComponentBeingTested
]
})
.compileComponents()
}))
Vastaus #2
Kommenttini jälkeen:
Kun haluat yksikkötestata reitittimesi, sinun täytyy käyttää testausmoduulia, ei varsinaista moduulia.
Aloita
import { RouterTestingModule } from '@angular/router/testing';
Sitten, testialustassasi
imports: [RouterTestingModule]
Nyt sinun pitäisi pystyä yksikkötestaamaan komponenttiasi
MUOKKAA
Vakoillaksesi reititystäsi, sinun täytyy tehdä seuraava
spyOn(component.router, 'navigate').and.returnValue(true);
Ja sinä odotat näyttää siltä
expect(component.router.navigate).toHaveBeenCalledWith('/home/advisor');
