【问题标题】:How cant test Query, Mutation, Subscription services using Apollo Angular?如何使用 Apollo Angular 测试查询、变异、订阅服务?
【发布时间】:2020-08-31 20:34:57
【问题描述】:

我看到了testing apollo in angular 工作原理的示例,基本上只使用ApolloTestingModule 进行测试。我的测试看起来像:

测试服


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();
  });
});

服务

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

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

问题

我想使用 Query, Mutation, Subscription service 的方法,但是用这种方法测试不起作用。

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

错误

● 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 } });

对我来说,这个错误是有道理的,因为在 Query class 的官方实现中,方法 fetch 和 watch 使用的是 Apollo 服务提供的 use 方法

问题

  • 是否有替代方法来测试 apollo 提供的此类服务?
  • 如果我想使用这种方法,我应该将其作为基本服务进行测试?

等待你的回答

【问题讨论】:

    标签: angular apollo-client apollo-angular


    【解决方案1】:

    尝试实例化你的服务 并使用methode countryService.watch() 调用它 那么

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

    【讨论】:

    • 我不明白,因为那行代码和我在测试服里的一样
    【解决方案2】:

    我能够使用这种在提供程序中模拟服务的方法使其工作(不使用 AppolloTestingModule)。制作了一个辅助函数graphQlServiceMock,以便在所有服务中重用。

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

    或者没有助手..

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

    【讨论】:

    猜你喜欢
    • 2021-01-18
    • 2020-04-21
    • 1970-01-01
    • 2019-05-13
    • 2017-12-25
    • 2019-07-11
    • 1970-01-01
    • 2021-05-28
    • 2017-08-12
    相关资源
    最近更新 更多