【发布时间】: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