[英]Cannot match any routes. Error in angular?
I made a simple app
which is running correctly. 我做了一个简单的
app
,它可以正常运行。
Now I am trying to write test cases of that application, so I tried with routing
. 现在,我正在尝试编写该应用程序的测试用例,因此我尝试使用
routing
。
My routing code is this 我的路由代码是这样
Main module: 主模块:
export const routes: Routes = [
{ path: '', redirectTo: '/users', pathMatch: 'full' },
];
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
UserModule,
HttpClientModule,
RouterModule.forRoot(routes)
],
Feature module: 功能模块:
const routes: Routes = [
{path: 'users', component: ListingComponent}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(routes)
],
declarations: [ListingComponent]
})
Code 码
I try to run my spec
but I am getting above error 我尝试运行我的
spec
但出现错误
describe('Initial navigation', () => {
it('default route redirects to home (async)', fakeAsync(() => {
router.initialNavigation(); // triggers default
fixture.detectChanges();
tick();
console.log('==================');
console.log(location.path());
// fixture.whenStable().then(() => {
expect(location.path()).toBe('/users');
// })
}));
});
If you import UserModule
to the spec, this resolves the error. 如果将
UserModule
导入规范,则可以解决该错误。 As AppModule
modules imports UserModule
to register user feature/module routes, it must also be imported in the spec to ensure it's route registrations are available in the spec as well. 当
AppModule
模块导入UserModule
以注册用户功能/模块路线时,还必须将其导入规范中,以确保其路线注册在规范中也可用。
The need for this is implied at a basic level in the documentation Testing: Import a feature module . 在文档“ 测试:导入功能模块”中基本隐含了对此的需求。
//setup
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
UserModule, // import module
RouterTestingModule.withRoutes(routes)],
declarations: [
TestComponent,
]
});
fixture = TestBed.createComponent(TestComponent);
router = TestBed.get(Router);
location = TestBed.get(Location);
});
Here is an updated StackBlitz demonstrating the functionality (test passing with no error). 这是演示功能的更新StackBlitz (测试通过,无错误)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.