error:NullInjectorError: No provider for Router
error:NullInjectorError: No provider for Router
我必须测试一个组件并收到此错误 NullInjectorError: R3InjectorError(DynamicTestModule)[AuthenticationService -> Router -> Router]:
NullInjectorError:路由器没有提供者!**”
我正在测试的组件有两个依赖项,我不知道是否可以在测试台的测试中同时提供它们
gallery.component.ts
constructor( private authService:AuthenticationService, private iterableDiffers: IterableDiffers){
this.iterableDiffer = iterableDiffers.find([]).create(null);
}
gallery.component.spec.ts
describe('GalleryComponent', () => {
let component: GalleryComponent;
let fixture: ComponentFixture<GalleryComponent>
let authServiceMock: AuthenticationService
let iterableDifferMock: IterableDiffers
beforeEach(() => {
TestBed.configureTestingModule({
providers:[GalleryComponent, {provide:AuthenticationService}, {provide:IterableDiffers}]
})
fixture = TestBed.createComponent(GalleryComponent);
component = fixture.componentInstance;
authServiceMock = TestBed.inject(AuthenticationService)
})
...
如何提供这两个依赖项?
我阅读了 Angular DOC,但没有找到解决方案,我在 SO 上提出了其他问题,但没有找到解决方案。
谢谢
您只需在组件的构造函数中注入您拥有的所有依赖项:
constructor( private authService:AuthenticationService, private iterableDiffers: IterableDiffers){}
如果您也需要使用 Angular 路由器:
constructor( private authService:AuthenticationService, private iterableDiffers: IterableDiffers, private router: Router){}
无需添加:
this.iterableDiffer = iterableDiffers.find([]).create(null);
之后,您可以调用并使用您想要的依赖项。
只需将 RouterTestingModule
导入 TestBed 中的导入数组
TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers:[
GalleryComponent,
{provide:AuthenticationService},
{provide:IterableDiffers}
]
})
我必须测试一个组件并收到此错误 NullInjectorError: R3InjectorError(DynamicTestModule)[AuthenticationService -> Router -> Router]: NullInjectorError:路由器没有提供者!**” 我正在测试的组件有两个依赖项,我不知道是否可以在测试台的测试中同时提供它们 gallery.component.ts
constructor( private authService:AuthenticationService, private iterableDiffers: IterableDiffers){
this.iterableDiffer = iterableDiffers.find([]).create(null);
}
gallery.component.spec.ts
describe('GalleryComponent', () => {
let component: GalleryComponent;
let fixture: ComponentFixture<GalleryComponent>
let authServiceMock: AuthenticationService
let iterableDifferMock: IterableDiffers
beforeEach(() => {
TestBed.configureTestingModule({
providers:[GalleryComponent, {provide:AuthenticationService}, {provide:IterableDiffers}]
})
fixture = TestBed.createComponent(GalleryComponent);
component = fixture.componentInstance;
authServiceMock = TestBed.inject(AuthenticationService)
})
...
如何提供这两个依赖项?
我阅读了 Angular DOC,但没有找到解决方案,我在 SO 上提出了其他问题,但没有找到解决方案。
谢谢
您只需在组件的构造函数中注入您拥有的所有依赖项:
constructor( private authService:AuthenticationService, private iterableDiffers: IterableDiffers){}
如果您也需要使用 Angular 路由器:
constructor( private authService:AuthenticationService, private iterableDiffers: IterableDiffers, private router: Router){}
无需添加:
this.iterableDiffer = iterableDiffers.find([]).create(null);
之后,您可以调用并使用您想要的依赖项。
只需将 RouterTestingModule
导入 TestBed 中的导入数组
TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers:[
GalleryComponent,
{provide:AuthenticationService},
{provide:IterableDiffers}
]
})