我正在测试一个使用阿波罗和graphql的角度服务,我收到了No provider for HttpClient! Error,尽管我在导入中添加了HttpClientTestingModule。
rules.spec.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()
}))
})
发布于 2019-02-01 10:12:41
通常,提供ApolloTestingModule就足够了。我在关于阿波罗测试https://medium.com/@sergeyfetiskin/testing-apollo-graphql-in-your-angular-application-595f0a04aad3的文章中也是这样做的,在日常工作中也是如此。
HTTPClient的需求可能不是由阿波罗本身造成的,而是由直接使用它的一些服务引起的。因此,检查哪个服务使用它,并模拟该服务。
我有点惊讶,HTTPClientTestingModule没有帮助,但我重复一遍,它与阿波罗无关。
发布于 2019-05-22 08:43:52
您还需要导入HttpClientModule。阿波罗客户端需要该模块来提供HttpClient。
https://stackoverflow.com/questions/54464607
复制相似问题