Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am preparing unit test case for AppComponent which have router as a injected dependency and have included RouterTestingModule in my test bed. But still getting a weird error. Please find the error log shown below:

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

error properties: 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: null }), Object({ ... Error: StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]: StaticInjectorError(Platform: core)[RouterLinkWithHref -> Router]: NullInjectorError: No provider for 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) at tryResolveToken (webpack:///./node_modules/@angular/core/fesm5/core.js?:1542:16) at StaticInjector.get (webpack:///./node_modules/@angular/core/fesm5/core.js?:1439:20) at resolveToken (webpack:///./node_modules/@angular/core/fesm5/core.js?:1598:24) at tryResolveToken (webpack:///./node_modules/@angular/core/fesm5/core.js?:1542:16) at StaticInjector.get (webpack:///./node_modules/@angular/core/fesm5/core.js?:1439:20) at resolveNgModuleDep (webpack:///./node_modules/@angular/core/fesm5/core.js?:8667:29) at NgModuleRef_.get (webpack:///./node_modules/@angular/core/fesm5/core.js?:9355:16) at resolveDep (webpack:///./node_modules/@angular/core/fesm5/core.js?:9720:45)

Please help. I have already tried removing router links from my template.

TestBed.configureTestingModule({
    declarations: [
      AppComponent
    ],
    imports: [
      CoreModule.forRoot(),
      RouterTestingModule.withRoutes(routes),
    ],
    providers: [
      {provide: APP_BASE_HREF, useValue: '/'},
    ]
}
like image 344
Aakash Garg Avatar asked Jan 29 '19 06:01

Aakash Garg


1 Answers

May I know if you have imported RouterTestingModule? You should import it this way:

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

Also, what is routes in your RouterTestingModule.withRoutes(routes)? Here is a sample of how you can import RouterTestingModule into your Testbed.

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

  })
    .compileComponents()
}))
like image 90
wentjun Avatar answered Nov 07 '22 07:11

wentjun