angular 单元测试报错:Failed: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?).

测试中如果不关心真实的路由跳转,可以使用mock

let mockRouter = {
  navigate: jasmine.createSpy('navigate')
} 

TestBed.configureTestingModule({
  providers: [
    { provide: Router, useValue: mockRouter }
  ]
})

在测试中可能只是想要确认navigate被调用,可以这样:

expect(mockRouter.navigate).toHaveBeenCalledWith(['/router']);

如果想要测试真实导航,可以使用RouterTestingModule,添加所有的路由providersdirectives

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

你可能感兴趣的:(angular 单元测试报错:Failed: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?).)