gpt4 book ai didi

angular - 失败 : Expected one matching operation for criteria "Match DocumentNode", 找不到

转载 作者:行者123 更新时间:2023-11-28 20:26:26 31 4
gpt4 key购买 nike

我正在尝试为与 graphql 和 Apollo 一起使用的 Angular 服务编写测试。遵循本指南:apollo-testing

感谢您的帮助!

我收到此错误:失败:需要一个条件“匹配”的匹配操作 DocumentNode”,找不到。

规则.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()
})


})

最佳答案

不确定您是否仍然遇到此问题,但我能够使用问题中的此评论解决它: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' }))
}
});

关于angular - 失败 : Expected one matching operation for criteria "Match DocumentNode", 找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54449365/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com