Angular testing and HttpClient: TypeError: Cannot read property 'get' of undefined

Angular testing and HttpClient: TypeError: Cannot read property 'get' of undefined

在我的 Angular 4 应用程序中,我需要为使用内部使用 angular HttpClient 的服务的组件创建测试。

尽管我在 TestBedimports 之间添加了 HttpClientTestingModule,但我得到:

TypeError: Cannot read property 'get' of undefined

每当组件调用 MyService.someMethod()(内部使用 HttpClient.get())时。

我尝试提供服务本身或为其提供模拟,但我还是遇到了错误。

我发现了问题:

我需要从 TestBedproviders 列表中删除 { provide: AuthService, useClass: MockAuthService }。它不再需要并且以某种方式干扰了 HttpClientTestingModule.