Angular 7 Test: NullInjectorError: No provider for ActivatedRoute Angular 7 Test: NullInjectorError: No provider for ActivatedRoute angular angular

Angular 7 Test: NullInjectorError: No provider for ActivatedRoute


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.