【问题标题】:Error: StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]: (NullInjectorError: No provider for Router!)错误:StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]:(NullInjectorError:没有路由器的提供者!)
【发布时间】:2019-06-22 05:59:27
【问题描述】:

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

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

错误属性:Object({ ngTempTokenPath: null, ngTokenPath: [ 'RouterLinkWithHref', 函数], ngDebugContext: DebugContext_({ 视图:对象({定义:对象({工厂:函数,nodeFlags:671753, rootNodeFlags:1,nodeMatchedQueries:0,标志:0,节点:[对象({ nodeIndex: 0, parent: null, renderParent: null, bindingIndex: 0, outputIndex:0,checkIndex:0,标志:1,childFlags:671753, directChildFlags:1,childMatchedQueries:0,matchedQueries:对象({ }),matchedQueryIds:0,引用:对象({}),ngContentIndex: 空,childCount:10,绑定:[对象({标志:8,ns:'',名称: 'className',nonMinifiedName:'className',securityContext:0,后缀: undefined }) ], bindingFlags: 8, 输出: [ ], element: Object({ ns: '',名称:'nav',attrs:[数组],模板:null,componentProvider: 空,componentView:空,componentRendererType:空, 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: '/'},
    ]
}

【问题讨论】:

  • 以防万一,尝试创建像TestBed.overrideComponent(AppComponent, { set: { template: '' }}).createComponent(AppComponent);这样的组件并检查该错误是否仍然存在。
  • 那你试过了吗?
  • 让我试试看是否有效。
  • 此解决方案无效
  • 好的,可以添加CoreModule的列表吗?

标签: angular karma-jasmine


【解决方案1】:

请问您是否导入了RouterTestingModule?你应该这样导入它:

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

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

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

  })
    .compileComponents()
}))

【讨论】:

  • 我们应该在哪个文件中更新以解决此问题
  • @MohanGopi 它应该在需要使用模拟路由器的测试/规范文件中
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2022-11-25
    • 2020-08-15
    • 2022-07-24
    • 2018-10-19
    • 1970-01-01
    • 2018-12-27
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多