简体   繁体   English

如何使用 Apollo Angular 测试查询、变异、订阅服务?

[英]How cant test Query, Mutation, Subscription services using Apollo Angular?

I saw the examples of how testing apollo in angular works and basically only use the ApolloTestingModule to make the tests.在 angular 中看到了测试 apollo的示例,基本上只使用ApolloTestingModule进行测试。 And my test looks like:我的测试看起来像:

Test suit测试服


describe('CountriesService', () => {
  let countryService: CountriesService;
  let controller: ApolloTestingController;
  let scheduler: TestScheduler;
  let countries: any;

  beforeAll(() => {
    TestBed.configureTestingModule({
      imports: [ApolloTestingModule],
    });

    countries = [{ code: 'C1', phone: '500', name: 'Country' }];
    scheduler = new TestScheduler((actual, expected) => {
      expect(actual).toEqual(expected);
    });
    countryService = TestBed.inject(CountriesService);
    controller = TestBed.inject(ApolloTestingController);
  });

  test('should be created', () => {
    expect(countryService).toBeTruthy();
  });

  test('should return an array of countries', () => {
    countryService.watch().valueChanges.subscribe(console.log);

    const operation = controller.expectOne(COUNTRIES_GQL);
    operation.flush({ data: { countries } });

    controller.verify();
  });
});

Service服务

@Injectable({ providedIn: 'root' })
export class CountriesService {
  constructor(private apollo: Apollo) {}

  watch() {
    return this.apollo.watchQuery({
      query: COUNTRIES_GQL,
    });
  }
}

Problem问题

I want to use the approach of using Query, Mutation, Subscription service but with this approach the test doesn't work.我想使用使用查询、变异、订阅服务的方法,但是使用这种方法测试不起作用。

@Injectable({ providedIn: 'root' })
export class CountriesService extends Query<any> {
  document = COUNTRIES_GQL;
}

Error错误

● CountriesService › should return an array of countries

    TypeError: Cannot read property 'use' of undefined

      30 | 
      31 |   test('should return an array of countries', () => {
      32 |     countryService.watch().valueChanges.subscribe(console.log);
         |                    ^
      33 | 
      34 |     const operation = controller.expectOne(COUNTRIES_GQL);
      35 |     operation.flush({ data: { countries } });

For me the error makes sense because in the official implementation of Query class the methods fetch and watch are using the use method provided by Apollo service.对我来说,这个错误是有道理的,因为在查询 class的官方实现中,方法 fetch 和 watch 使用的是 Apollo 服务提供的使用方法

Questions问题

  • Does an alternative to test this type of service provided by apollo?是否可以替代测试 apollo 提供的此类服务?
  • If I want to use this approach, I should test it as a basic service?如果我想使用这种方法,我应该将它作为一项基本服务进行测试?

I wait for your answers我等你的答案

try to instance ur service and call it with the methode countryService.watch() then尝试实例化你的服务并使用methode countryService.watch()然后调用它

countryService.watch().valueChanges.subscribe(console.log);

I was able to get it working using this approach mocking the service in providers (does not use AppolloTestingModule).我能够使用这种方法使其工作 mocking 提供者中的服务(不使用 AppolloTestingModule)。 Made a helper function graphQlServiceMock for reuse across all services.制作了一个助手 function graphQlServiceMock以便在所有服务中重用。

TestBed.configureTestingModule({
  ...
  providers: [
    {
       provide: MyGQLService,
       useValue: graphQlServiceMock({ users: null }),
    },
  ]

})
export const graphQlServiceMock = (response: any) => ({
  watch: () => ({
    valueChanges: of({
      data: {
        ...response,
      },
      loading: false,
    }),
  }),
});

or without helper..或没有帮手..

TestBed.configureTestingModule({
  ...
  providers: [
  {
    provide: MyGQLService,
    useValue: {
      watch: () => ({
        valueChanges: of({
          data: {
            users: null,
          },
          loading: false,
        }),
      }),
    },
  },
 ];
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM