Angular 7 Test: NullInjectorError: No Provider For ActivatedRoute


Answer :

You have to import RouterTestingModule

import { RouterTestingModule } from "@angular/router/testing";  imports: [     ...     RouterTestingModule     ... ] 

Add the following import

  imports: [      RouterModule.forRoot([]),     ...   ], 

I had this error for some time as well while testing, and none of these answers really helped me. What fixed it for me was importing both the RouterTestingModule and the HttpClientTestingModule.

So essentially it would look like this in your whatever.component.spec.ts file.

  beforeEach(async(() => {     TestBed.configureTestingModule({       declarations: [ProductComponent],       imports: [RouterTestingModule, HttpClientTestingModule],     }).compileComponents();   })); 

You can get the imports from

import { RouterTestingModule } from "@angular/router/testing"; import { HttpClientTestingModule } from "@angular/common/http/testing"; 

I dont know if this is the best solution, but this worked for me.


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable