NullInjectorError:没有 HttpClient 的提供者

Mot*_*ani 1 unit-testing apollo karma-jasmine graphql angular

我正在测试使用 Apollo 和 graphql 的角度服务,我收到No provider for HttpClient! 即使我将 HttpClientTestingModule 添加到导入中也会出错。

规则.规范.ts

import { PlatformGraphQLService } from 'platform-graphql'
import { TestBed, ComponentFixture } from '@angular/core/testing'
import { RulesService } from './rules.service'
import {
  ApolloTestingModule,
  ApolloTestingController
} from 'apollo-angular/testing'
import { async } from '@angular/core/testing'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { Apollo, ApolloModule } from 'apollo-angular'

describe('RulesService', () => {
  let controller: ApolloTestingController
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        ApolloTestingModule,
        HttpClientTestingModule,
      ],
      providers: [
        PlatformGraphQLService,
      ],
    })
    controller = TestBed.get(ApolloTestingController)
  })

  it('should be created', async(() => {
    const service: RulesService = TestBed.get(RulesService)
    expect(service).toBeTruthy()
  }))


})
Run Code Online (Sandbox Code Playgroud)

s-f*_*s-f 5

通常情况下,提供ApolloTestingModule就足够了。我在关于 Apollo 测试的文章https://medium.com/@sergeyfetiskin/testing-apollo-graphql-in-your-angular-application-595f0a04aad3中也做了同样的事情,我在日常工作中也做了同样的事情。

需求HTTPClient可能不是由 Apollo 本身引起的,而是由直接使用它的某些服务引起的。因此,检查哪个服务使用它并模拟该服务。

我有点惊讶这HTTPClientTestingModule没有帮助,但我再说一遍,它与阿波罗无关。