11
votes

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: '/'},
    ]
}
2
Just in case, try to create component like TestBed.overrideComponent(AppComponent, { set: { template: '' }}).createComponent(AppComponent); and check whether that error stays or not.Amir Arbabian
So have you tried that?Amir Arbabian
Let me try it and see if it works.Aakash Garg
This solution didn't workAakash Garg
Ok, can you add the listing of CoreModule, please?Amir Arbabian

2 Answers

31
votes

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()
}))
-2
votes

I was able to resolve this issue. While modifying test cases i updated version of @types/jasmine to resolve some issue and updated some other dependencies as well. In the background my application also broke due to that. So, i re-forked the repository and then updated required dependencies only. Now code is working fine.