失败:对于条件“Match DocumentNode”,未找到任何匹配操作

问题描述 投票:1回答:1

我正在尝试为一个角度服务编写测试,我正在使用graphql和Apollo。遵循本指南:apollo-testing

我感谢您的帮助!

我收到此错误:失败:对标准“匹配DocumentNode”预期一个匹配操作,找不到。

rules.spec.ts

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

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

  it('should return a rule from server', async(() => {
    const service: RulesService = TestBed.get(RulesService)

    service.loadRules()
      .valueChanges.subscribe(() => {
        expect(op.operation.variables.consequent.isExluded).toEqual(true)
      })

    const op = controller.expectOne(GET_RULE_QUERY)
    console.log(op)
    op.flush({
      'conditions': [
        {
          'glItemType': 'DEPARTMENT',
          'operation': 'LEQ',
          'value': 1300,
        },
        {
          'glItemType': 'SUBDEPARTMENT',
          'operation': 'GEQ',
          'value': 4805,
        }
      ],
      'consequent': {
        'isExluded': true,
      },
    })

  }))

  afterEach(() => {
    controller.verify()
  })


})
angular testing graphql karma-jasmine apollo
1个回答
1
投票

不确定你是否仍然遇到这个问题,但我能够在一个问题中使用这个评论来解决它:https://github.com/apollographql/apollo-angular/issues/691#issuecomment-417293424

它归结为三件:

  1. 向测试模块添加其他提供程序
TestBed.configureTestingModule({
  ...
  providers: [
    ...
    {
      provide: APOLLO_TESTING_CACHE,
      useValue: new InMemoryCache({addTypename: true}),
    },
  ] 
});
  1. 在调用expectOne时将查询包装在addTypenameToDocument
const op = controller.expectOne(addTypenameToDocument(getClientsQuery));
  1. 将适当的__typename添加到模拟数据中......我相信你的情况看起来像这样:
op.flush({
  'conditions': [
    {
      'glItemType': 'DEPARTMENT',
      'operation': 'LEQ',
      'value': 1300,
      '__typename': 'DefinedOnBackend'
    },
    {
      'glItemType': 'SUBDEPARTMENT',
      'operation': 'GEQ',
      'value': 4805,
      '__typename': 'DefinedOnBackend'
    }
  ],
  'consequent': {
    'isExluded': true,
    '__typename': 'DefinedOnBackend'
  },
})

对我来说它看起来像这样(基本上为每个返回的记录添加__typename):

op.flush({
  data: {
    getClients: clients.map(c => ({ ...c, __typename: 'ClientDTO' }))
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.