And*_*cio 4 unit-testing jasmine angular-providers angular
我必须测试一个组件并收到此错误 NullInjectorError: R3InjectorError(DynamicTestModule)[AuthenticationService -> Router -> Router]: NullInjectorError: No provider for Router!**" 我正在测试的组件有两个依赖项,但我没有知道 hot 在测试中使用 testbed gallery.component.ts提供它们
constructor( private authService:AuthenticationService, private iterableDiffers: IterableDiffers){
this.iterableDiffer = iterableDiffers.find([]).create(null);
}
Run Code Online (Sandbox Code Playgroud)
画廊.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)
})
...
Run Code Online (Sandbox Code Playgroud)
如何提供这两个依赖项?
我阅读了 Angular DOC,但没有找到解决方案,我在 SO 上提出了其他问题,但没有找到解决方案。
谢谢
只需导入RouterTestingModule到 TestBed 中的导入数组
TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers:[
GalleryComponent,
{provide:AuthenticationService},
{provide:IterableDiffers}
]
})
Run Code Online (Sandbox Code Playgroud)