【问题标题】:Failed: Expected one matching operation for criteria "Match DocumentNode", found none失败:需要对条件“匹配 DocumentNode”进行一次匹配操作,但没有找到
【发布时间】:2019-06-24 06:02:49
【问题描述】:

我正在尝试为我与 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】:

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

    归结为三部分:

    1. 向测试模块添加额外的提供程序
    TestBed.configureTestingModule({
      ...
      providers: [
        ...
        {
          provide: APOLLO_TESTING_CACHE,
          useValue: new InMemoryCache({addTypename: true}),
        },
      ] 
    });
    
    1. 在对addTypenameToDocument 的调用中将查询包装在expectOne
    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' }))
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 2017-12-25
      • 2012-09-19
      • 1970-01-01
      • 2021-05-13
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多