Reputation: 7606
I just upgraded to Angular RC 5 and now all component that uses 'ROUTER_DIRECTIVES' fails with 'Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?)' when I try to unit test the component.
import { inject, addProviders } from '@angular/core/testing';
import { ComponentFixture, TestComponentBuilder } from '@angular/core/testing';
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES, Router } from '@angular/router';
import { HomeComponent } from './home.component';
import { UserService } from '../_services/user.service';
describe('Component: Home', () => {
beforeEach(() => {
addProviders([HomeComponent, UserService, ROUTER_DIRECTIVES, Router]);
});
it('should inject the component', inject([HomeComponent, UserService, ROUTER_DIRECTIVES, Router],
(component: HomeComponent) => {
expect(component).toBeTruthy();
// expect(component.currentUser.firstname).toEqual('Jan');
}));
The full error log:
Chrome 52.0.2743 (Windows 10 0.0.0)
Error: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?).
at new BaseException (webpack:///C:/ng/anbud/~/@angular/compiler/src/facade/exceptions.js:27:0 <- src/test.ts:2943:23)
at CompileMetadataResolver.getDependenciesMetadata (webpack:///C:/ng/anbud/~/@angular/compiler/src/metadata_resolver.js:551:0 <- src/test.ts:24542:19)
at CompileMetadataResolver.getTypeMetadata (webpack:///C:/ng/anbud/~/@angular/compiler/src/metadata_resolver.js:448:0 <- src/test.ts:24439:26)
at webpack:///C:/ng/anbud/~/@angular/compiler/src/metadata_resolver.js:594:0 <- src/test.ts:24585:41
at Array.forEach (native)
at CompileMetadataResolver.getProvidersMetadata (webpack:///C:/ng/anbud/~/@angular/compiler/src/metadata_resolver.js:575:0 <- src/test.ts:24566:19)
at CompileMetadataResolver.getNgModuleMetadata (webpack:///C:/ng/anbud/~/@angular/compiler/src/metadata_resolver.js:305:0 <- src/test.ts:24296:58)
at RuntimeCompiler._compileComponents (webpack:///C:/ng/anbud/~/@angular/compiler/src/runtime_compiler.js:150:0 <- src/test.ts:37986:47)
at RuntimeCompiler._compileModuleAndAllComponents (webpack:///C:/ng/anbud/~/@angular/compiler/src/runtime_compiler.js:78:0 <- src/test.ts:37914:37)
at RuntimeCompiler.compileModuleAndAllComponentsSync (webpack:///C:/ng/anbud/~/@angular/compiler/src/runtime_compiler.js:52:0 <- src/test.ts:37888:21)
Any ideas how to unit test components with routing?
Upvotes: 58
Views: 42961
Reputation: 8242
For latest Angular 7, here is the solution that worked for me. The example is copied from latest angular docs.
app/dashboard/dashboard.component.spec.ts (spies)
const routerSpy = jasmine.createSpyObj('Router', ['navigateByUrl']);
const heroServiceSpy = jasmine.createSpyObj('HeroService', ['getHeroes']);
TestBed.configureTestingModule({
providers: [
{ provide: HeroService, useValue: heroServiceSpy },
{ provide: Router, useValue: routerSpy }
]
})
app/dashboard/dashboard.component.spec.ts (navigate test)
it('should tell ROUTER to navigate when hero clicked', () => {
heroClick(); // trigger click on first inner <div class="hero">
// args passed to router.navigateByUrl() spy
const spy = router.navigateByUrl as jasmine.Spy;
const navArgs = spy.calls.first().args[0];
// expecting to navigate to id of the component's first hero
const id = comp.heroes[0].id;
expect(navArgs).toBe('/heroes/' + id,
'should nav to HeroDetail for first hero');
});
Upvotes: 3
Reputation: 2249
I was getting the same error, importing RouterTestModule in my imports array, and removing Router/ActivatedRoute from providers and also removing RouterModule from my imports array helped me to get over this issue.
This is how my working version looks like
import { RouterTestingModule } from '@angular/router/testing';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { MyAppComponent } from './my-apppp.component';
import { MyAppService } from '../../../services/my-app.service';
import { NO_ERRORS_SCHEMA} from '@angular/core';
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MyAppComponent ],
schemas: [NO_ERRORS_SCHEMA],
imports: [ FormsModule, RouterTestingModule ],
providers: [ MyAppService ]
})
.compileComponents();
}));
I know a lot of people already commented in here, but I felt like providing a updated answer would be helpful.
Thanks!
Upvotes: 5
Reputation: 5141
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}
]
});
Upvotes: 24
Reputation: 526
I just had to remove the router from the providers. That was causing the problem for me.
Upvotes: 9
Reputation: 1454
In my case, I had Router in providers and RouterTestingModule in imports. Guess this was causing a conflict. The following configuration worked:
TestBed.configureTestingModule({
declarations: [Component],
imports: [
RouterTestingModule,
],
providers: [
... //Remove Router from providers
]
});
Upvotes: 28
Reputation: 3155
for me it worked to add the RouterTestingModule
import { RouterTestingModule } from '@angular/router/testing';
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
declarations: [ HomeComponent ]
})
.compileComponents();
}));
Upvotes: 50
Reputation: 139
Try adding RouterModule:
import { ROUTER_DIRECTIVES, Router, RouterModule } from '@angular/router';
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
...
{provide: Router, useClass: RouterModule},
});
});
Upvotes: 11
Reputation: 7606
Was finally able to fix it, and it was as simple as this:
beforeEach(() => addProviders([
{
provide: Router,
useClass: class { navigate = jasmine.createSpy("navigate"); }
}]));
Upvotes: 53