首页 > 解决方案 > 无法读取未定义的属性“_lastPathIndex”

问题描述

我在我的组件规范中模拟激活路由,如下所示:

class ActvatedRouterMock {
  public paramMap = of(convertToParamMap({
    level: 'customer',
    id: '12345',
  }));
}

在提供者部分也添加这个类。

但是当我运行我的测试用例时,我收到错误无法读取_lastPathIndex未定义的属性

版本:Angular8 任何人都可以帮助我解决这个问题吗?

标签: angularunit-testingjasminerouter

解决方案


可能在您的生产代码中,您有类似的内容:

    this.router.navigate(['../'], {
      relativeTo: this.route
    });

然后你需要为snapshot你激活的路由提供一个属性。

例子:

class ActvatedRouterMock {
  public paramMap = of(convertToParamMap({
    level: 'customer',
    id: '12345',
  }));
  public snapshot = new ActivatedRouteSnapshot();
}

推荐阅读