我正在为AppComponent准备单元测试用例,它将路由器作为注入依赖项,并在我的测试台中包含了RouterTestingModule。但仍然得到一个奇怪的错误。请找到如下所示的错误日志:
Error: StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]:
StaticInjectorError(Platform: core)[RouterLinkWithHref -> Router]:
NullInjectorError: No provider for Router!
error属性:Object({ngTempTokenPath:null,ngTokenPath:['RouterLinkWithHref',Function],ngDebugContext:DebugContext _({view:Object({def:Object({factory:Function,nodeFlags:671753,rootNodeFlags:1,nodeMatchedQueries:0 ,flags:0,nodes:[Object({nodeIndex:0,parent:null,renderParent:null,bindingIndex:0,outputIndex:0,checkIndex:0,flags:1,childFlags:671753,directChildFlags:1,childMatchedQueries:0 ,matchedQueries:Object({}),matchedQueryIds:0,references:Object({}),ngContentIndex:null,childCount:10,bindings:[Object({flags:8,ns:'',name:'className', nonMinifiedName:'className',securityContext:0,suffix:undefined})],bindingFlags:8,outputs:[],element:Object({ns:'',name:'nav',attrs:[Array],template: null,componentProvider:null,componentView:null,componentRendererType:null,publicProviders:null({}),allProviders:null({}),handleEvent:Function}),provider:null,text:null,query:null,ngContent:空值 }), Object({... Error:StaticInjectorError(DynamicTestModule)[RouterLinkWithHref - > Router]:StaticInjectorError(Platform:core)[RouterLinkWithHref - > Router]:NullInjectorError:没有Router的提供者! at NullInjector.get(webpack:///./node_modules/@angular/core/fesm5/core.js?:1360:19)at resolveToken(webpack:///./node_modules/@angular/core/fesm5/core .js?:1598:24)在StaticInjector.get(webpack:///。/ node_modules /)的tryResolveToken(webpack:///./node_modules/@angular/core/fesm5/core.js?:1542:16) @ angular / core / fesm5 / core.js?:1439:20)在resolveRoken(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)在NgModuleRef_.get的resolveNgModuleDep(webpack:///./node_modules/@angular/core/fesm5/core.js?:8667:29)(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: '/'},
]
}
我可以知道你是否进口了RouterTestingModule
?你应该这样导入:
import { RouterTestingModule } from '@angular/router/testing';
另外,你的routes
中的RouterTestingModule.withRoutes(routes)
是什么?以下是如何将RouterTestingModule导入Testbed的示例。
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
RouterTestingModule.withRoutes([]),
],
declarations: [
SomeComponent
],
providers: [
SampleService,
]
})
.compileComponents();
}));
我能够解决这个问题。在修改测试用例时,我更新了@ types / jasmine的版本以解决某些问题并更新了一些其他依赖项。在后台我的应用程序也因此而破产。所以,我重新分叉了存储库,然后只更新了所需的依赖项。现在代码工作正常。