javascript - Angular 单元测试 : Error: Cannot match any routes. URL 段: 'home/advisor'

标签 javascript angular typescript jasmine

我正在我的 angular 4.0.0 应用程序下进行单元测试,我的真实组件中的一些方法正在通过以下方式调用手动路由:

method(){
....
    this.navigateTo('/home/advisor');
....
}

with navigateTo 是一个自定义路由方法,调用它:

  public navigateTo(url: string) {
    this.oldUrl = this.router.url;
    this.router.navigate([url], { skipLocationChange: true });
  }

我有这个路由文件:

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 { }

奇怪的是,即使我的应用程序运行正常,但测试会抛出此错误:

Failed: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'home/advisor' Error: Cannot match any routes. URL Segment: 'home/advisor'

看来我缺少一些配置。

有什么想法吗??

最佳答案

你需要这样的RouterTestingModule.withRoutes:

beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [
      RouterTestingModule.withRoutes(
        [{path: 'yourpath', component: BlankComponent}]
      )
    ],
    declarations: [
      BlankComponent,
      YourComponentBeingTested
    ]
  })
  .compileComponents()
}))

关于javascript - Angular 单元测试 : Error: Cannot match any routes. URL 段: 'home/advisor',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47201037/

相关文章:

javascript - React-redux 持续存在,仅重新水合处于 redux 状态的某些节点

javascript - 如何隐藏/覆盖 iframe 的特定部分

angular - Nginx在Docker中:位置配置不起作用

http - 当 header 为 { 'Authorization' : 'Bearer ' + token } 时,req.body 为空

TypeScript Object.assign 混淆

javascript - 无法访问在 Angular 的另一种方法中以一种方法实例化的成员

javascript - Highcharts - 将条形图添加到饼图中

javascript - 如何使用firebased数据库事务?

angular - environment.ts 文件通常是提交还是忽略?

typescript - 为什么 Typescript 编译器不推断实例变量的作用域?