错误:StaticInjectorError(DynamicTestModule)[RouterLinkWithHref->路由器] :( NullInjectorError:没有路由器的提供者!)

时间:2019-01-29 06:57:36

标签: angular karma-jasmine

我正在为AppComponent准备单元测试用例,该组件具有将路由器作为注入的依赖项,并且在我的测试台中包括RouterTestingModule。但是仍然出现一个奇怪的错误。请找到如下所示的错误日志:

Error: StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]: 
  StaticInjectorError(Platform: core)[RouterLinkWithHref -> Router]: 
    NullInjectorError: No provider for Router!
  

错误属性:Object({ngTempTokenPath:null,ngTokenPath:[   'RouterLinkWithHref',函数],ngDebugContext:DebugContext _({   视图:Object({def:Object({factory:Function,nodeFlags:671753,   rootNodeFlags:1,nodeMatchedQueries:0,标志:0,节点:[Object({   nodeIndex:0,父级:null,renderParent:null,bindingIndex:0,   outputIndex:0,checkIndex:0,标志:1,childFlags:671753,   directChildFlags:1,childMatchedQueries:0,matchedQueries:Object({   }),matchedQueryIds:0,引用:Object({}),ngContentIndex:   null,childCount:10,绑定:[Object({标志:8,ns:“,名称:   'className',nonMinifiedName:'className',securityContext:0,后缀:   未定义})],bindingFlags:8,输出:[],元素:Object({ns:   '',名称:'nav',属性:[Array],模板:null,componentProvider:   null,componentView:null,componentRendererType:null,   publicProviders:null({}),allProviders:null({}),handleEvent:   函数}),提供程序:null,文本:null,查询:null,ngContent:null   }),Object({...错误:   StaticInjectorError(DynamicTestModule)[RouterLinkWithHref->路由器]:   StaticInjectorError(平台:核心)[RouterLinkWithHref->路由器]:       NullInjectorError:没有路由器的提供程序!       在NullInjector.get(webpack:///./node_modules/@angular/core/fesm5/core.js?:1360:19)       在resolveToken(webpack:///./node_modules/@angular/core/fesm5/core.js?:1598:24)       在tryResolveToken(webpack:///./node_modules/@angular/core/fesm5/core.js?:1542:16)       在StaticInjector.get(webpack:///./node_modules/@angular/core/fesm5/core.js?:1439:20)       在resolveToken(webpack:///./node_modules/@angular/core/fesm5/core.js?:1598:24)       在tryResolveToken(webpack:///./node_modules/@angular/core/fesm5/core.js?:1542:16)       在StaticInjector.get(webpack:///./node_modules/@angular/core/fesm5/core.js?:1439:20)       在resolveNgModuleDep(webpack:///./node_modules/@angular/core/fesm5/core.js?:8667:29)       在NgModuleRef_.get(webpack:///./node_modules/@angular/core/fesm5/core.js?:9355:16)       在resolveDep(webpack:///./node_modules/@angular/core/fesm5/core.js?:9720:45)

请帮助。我已经尝试从模板中删除路由器链接。

TestBed.configureTestingModule({
    declarations: [
      AppComponent
    ],
    imports: [
      CoreModule.forRoot(),
      RouterTestingModule.withRoutes(routes),
    ],
    providers: [
      {provide: APP_BASE_HREF, useValue: '/'},
    ]
}

2 个答案:

答案 0 :(得分:0)

我可以知道您是否导入了RouterTestingModule吗?您应该通过以下方式导入它:

import { RouterTestingModule } from '@angular/router/testing';

另外,您的routes中的RouterTestingModule.withRoutes(routes)是什么?这是如何将RouterTestingModule导入到测试平台的示例。

beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule
        RouterTestingModule.withRoutes([]),
      ],
      declarations: [
        SomeComponent
      ],
      providers: [           
        SampleService,
      ]

    })
    .compileComponents();
  }));

答案 1 :(得分:0)

我能够解决此问题。在修改测试用例时,我更新了@ types / jasmine的版本以解决某些问题并更新了其他一些依赖项。在后台,我的应用程序也因此而中断。因此,我重新分叉了存储库,然后仅更新了必需的依赖项。现在代码可以正常工作了。