测试中如果不关心真实的路由跳转,可以使用mock
let mockRouter = {
navigate: jasmine.createSpy('navigate')
}
TestBed.configureTestingModule({
providers: [
{ provide: Router, useValue: mockRouter }
]
})
在测试中可能只是想要确认navigate
被调用,可以这样:
expect(mockRouter.navigate).toHaveBeenCalledWith(['/router']);
如果想要测试真实导航,可以使用RouterTestingModule
,添加所有的路由providers
和directives
import { RouterTestingModule } from '@angular/router/testing';
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([{path: '', component: BlankCmp}, {path: 'simple', component: SimpleCmp}])
]
})
参考:# Angular 2 Unit testing: Can't resolve all parameters for Router