gpt4 book ai didi

angular - 错误 :NullInjectorError: No provider for Router

转载 作者:行者123 更新时间:2023-12-03 21:46:00 25 4
gpt4 key购买 nike

我必须测试一个组件并收到此错误 NullInjectorError: R3InjectorError(DynamicTestModule)[AuthenticationService -> Router -> Router]:
NullInjectorError:没有路由器提供程序!**”
我正在测试的组件有两个依赖项,我不知道在 testbed 的测试中同时提供这两个依赖项
画廊.component.ts

constructor( private authService:AuthenticationService, private iterableDiffers: IterableDiffers){
this.iterableDiffer = iterableDiffers.find([]).create(null);
}
画廊.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 上提出了其他问题,但没有找到解决方案。
谢谢

最佳答案

只需导入 RouterTestingModule到 TestBed 中的导入数组

TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers:[
GalleryComponent,
{provide:AuthenticationService},
{provide:IterableDiffers}
]
})

关于angular - 错误 :NullInjectorError: No provider for Router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64151259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com