Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?) in Angular RC 5 when unit testing
Was finally able to fix it, and it was as simple as this:
beforeEach(() => addProviders([
{
provide: Router,
useClass: class { navigate = jasmine.createSpy("navigate"); }
}]));
for me it worked to add the RouterTestingModule
import { RouterTestingModule } from '@angular/router/testing';
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
declarations: [ HomeComponent ]
})
.compileComponents();
}));
I did not have any luck with the above solutions. Instead, I followed the recommended approach in the official documentation: Testing Routed Components
First, create a stubbed router with whatever methods your component calls:
class RouterStub {
navigateByUrl(url: string) {
return url;
}
}
Then, when configuring your testing module you do the following:
TestBed.configureTestingModule({
declarations: [HeaderComponent],
providers: [
{provide: Router, useClass: RouterStub}
]
});