角度单元测试:错误:无法匹配任何路由。 URL 段:“主页/顾问”

2023-12-25

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

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

with 导航是一个调用此的自定义路由方法:

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

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

失败:未捕获(承诺):错误:无法匹配任何路由。网址 段:'home/advisor' 错误:无法匹配任何路由。网址段: “家庭/顾问”

看来我缺少一些配置。

有任何想法吗 ??


你需要RouterTestingModule.withRoutes像这样:

beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [
      RouterTestingModule.withRoutes(
        [{path: 'yourpath', component: BlankComponent}]
      )
    ],
    declarations: [
      BlankComponent,
      YourComponentBeingTested
    ]
  })
  .compileComponents()
}))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

角度单元测试:错误:无法匹配任何路由。 URL 段:“主页/顾问” 的相关文章

随机推荐

  • 以客户身份通过​​ iOS 应用程序登录 BigCommerce API

    我正在为 BigCommerce com 上的商店开发 iOS 应用程序 我已成功从 BigCommerce API 检索产品列表 并且还使用该列表创建了一个新用户 创建用户 https developer bigcommerce com
  • Flutter:自动路由:RouteGuard 在 AutoTabsScaffold 中不起作用

    我正在尝试为我的 AutoTabsScaffold bottom nav 添加身份验证防护 但它不起作用 它可以在其他导航页面中工作 但不仅仅在我的登陆页面内 其中 AutoTabsScaffold 底部导航位于 我在这里遗漏了什么吗 us
  • Apache mod_rewrite 域到子域?

    我有这个地址http www example com http www example com并有这个页面http www example com world http www example com world 我可以用 mod rewr
  • android:什么时候使用onStart()、onStop()?

    我读过几篇描述两者之间区别的帖子onStart and onResume onStart 当活动变得可见时调用 onResume 当活动准备好与用户交互时调用 美好的 我总是只是添加代码onPause and onResume 并且从不关心
  • htaccess子域

    如何使用 htaccess 做到这一点 subdomain domain com gt domain com subdomain no redirect on client side domain com subdomain gt subd
  • jqGrid 在第一次加载时排序

    我的网格有以下代码 我使用与数据源位于同一目录中的 XML 文件 var handsetGrid products jqGrid url catalog xml datatype xml colNames SKU Name Brand De
  • 操作错误:没有这样的表

    所以我正在开发我的应用程序 并向我的模型添加了一个 slugfield 然后像往常一样继续makemigrations 并且出现了巨大的红色错误墙 Traceback most recent call last File C Users A
  • 在开始编码之前如何规划我的基于网络的项目? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我和我的朋友开始作为合作伙伴一起工作 我们决定制作一个又一个的 Kick as 网站 我们写下了大约 100 个想法 是的 我们首先在
  • 如何在 python 中解压非常大的文件?

    使用 python 2 4 和内置ZipFile库 我无法读取非常大的 zip 文件 大于 1 或 2 GB 因为它想要将未压缩文件的全部内容存储在内存中 是否有另一种方法可以做到这一点 使用第三方库或其他一些黑客 或者我必须 掏出 并以这
  • Enzyme:如何测试作为 prop 传递的 onSubmit 函数?

    我对酶还很陌生 我有两个正在测试的组件 form jsx const LoginForm style handleSubmit gt return
  • 时间:2019-05-09 标签:c#sizeofdecimal?

    不清楚十进制类型的 sizeof 字节大小是否会像 SQL Server 中那样因精度而变化 C 类型的精度变量是 十进制 吗 我不想打开不安全代码来仅对十进制类型调用 sizeof 你会如何处理这个问题 十进制关键字表示 128 位数据类
  • 是否可以将 REST 和消息传递结合起来用于微服务?

    我们拥有基于微服务架构的应用程序的第一个版本 我们使用 REST 进行外部和内部通信 现在我们想从CP CAP定理 切换到AP 并使用消息总线进行微服务之间的通信 关于如何基于Kafka RabbitMQ等创建事件总线的信息有很多 但我找不
  • 如何在 Python 中使用 M2Crypto 重新创建以下签名命令行 OpenSSL 调用?

    这在命令行中完美运行 我想在 Python 代码中使用 M2Crypto 执行相同的操作 openssl smime binary sign signer certificate pem inkey key pem in some file
  • BLE血糖仪

    我正在尝试从血糖仪获取数据 但无法在互联网上找到有关实施的良好资源 这是我到目前为止能够实现的 我正在使用 BluetoothAdapter LeScanCallback 接口扫描设备 Override public void onLeSc
  • 如何下载地图选定部分的 OSM 图块

    我想使用 Openlayer OSM 图层以单个缩放级别离线下载地图的选定部分的地图 我有地图的四个角 即地图的显示部分 但需要获取所有图块图像或四个角之间的图块 我回顾了一些例子 Openlayers获取鼠标下图块的图片url https
  • 在.NET 4中使用await SemaphoreSlim.WaitAsync

    我的应用程序正在使用 NET 4 我正在使用等待异步努吉特包 https www nuget org packages Microsoft Bcl Async 在我的应用程序中 我想对信号量 WaitAsync 调用进行等待 如下所示 Se
  • 为什么 GHC 不能派生 Monoid 的实例?

    GHC 有一些语言标志 例如DeriveFunctor DeriveDataTypeable等等 这使得编译器能够为 Haskell 98 中允许的类型类之外的类型类生成派生实例 这对于类似的东西尤其有意义Functor 其中该类的定律规定
  • 绘制包含多个组件的图形时,节点大小不正确

    我有一个包含许多组件的图表 我想将其可视化 作为一个特殊特征 巨型组件中节点的节点点应随其特征向量中心性缩放 所有其他节点具有相同的大小 我使用以下脚本 import networkx as nx import pylab as py im
  • android recyclerview:如何以编程方式选择 TextView 背景颜色?

    我有一个 CardViews 的 RecyclerView 列表 在每个 CardView 上 用户事先从下拉对话框中选择 类型 类型选择有 工作 和 家庭 类型选择作为字符串存储在 SQLite 数据库中 当我运行应用程序时 没有显示 T
  • 角度单元测试:错误:无法匹配任何路由。 URL 段:“主页/顾问”

    我正在我的 Angular 4 0 0 应用程序下进行单元测试 我的真实组件中的某些方法是通过以下方式调用手动路由 method this navigateTo home advisor with 导航是一个调用此的自定义路由方法 publ