简体   繁体   中英

Flutter test GraphQL query

I want to test my GraphQL Query. I have my GraphQL client, and I use a remote datasource to do my requests.

class MockGraphQLClient extends Mock implements GraphQLClient {}

void main() {
  RemoteDataSource RemoteDataSource;
  MockGraphQLClient mockClient;

  setUp(() {
    mockClient = MockGraphQLClient();
    RemoteDataSource = RemoteDataSource(client: mockClient);
  });
  group('RemoteDataSource', () {
    group('getDetails', () {
      test(
          'should preform a query with get details with id variable',
          () async {
        final id = "id";
        when(
          mockClient.query(
            QueryOptions(
              documentNode: gql(Queries.getDetailsQuery),
              variables: {
                'id': id,
              },
            ),
          ),
        ).thenAnswer((_) async => QueryResult(
            data: json.decode(fixture('details.json'))['data'])));

        await RemoteDataSource.getDetailsQuery(id);

        verify(mockClient.query(
          QueryOptions(
            documentNode: gql(Queries.getDetailsQuery),
            variables: {
              'id': id,
            },
          ),
        ));
      });
    });
  });
}

I would like to know how to mock the response of my query. Currently it does not return a result, it returns null But I don't understand why my query returns null, although I have mocked my client, and in my "when" method I use a "thenAnwser" to return the desired value

final GraphQLClient client;

  ChatroomRemoteDataSource({this.client});

  @override
  Future<Model> getDetails(String id) async {
    try {
      final result = await client.query(QueryOptions(
        documentNode: gql(Queries.getDetailsQuery),
        variables: {
          'id': id,
        },
      )); // return => null ????

      if (result.data == null) {
        return [];
      }
      return result.data['details']
    } on Exception catch (exception) {
      throw ServerException();
    }
  }

The argument on which when should mock an answer for is quite complex. You might be easier to just use any in your test case.

when(mockClient.query(any)).thenAnswer((_) async => QueryResult(
        data: json.decode(fixture('details.json'))['data'])));

any is provided by Mockito to match any argument.

In the

graphql_flutter: ^5.0.0

you need the add source as null or QueryResultSource.network, when call method when can you pass any so you don't need to pass QueryOptions( documentNode: gql(Queries.getDetailsQuery), variables: { 'id': id, }, ) ,

here is final code: when(mockClient.query(any)).thenAnswer((_) async => QueryResult( data: json.decode(fixture('details.json'))['data'], ,source: null)));

any is not accepted with graphQLClient.query(any)) as it accepts non nullable QueryOptions<dynamic>

Using mockito: ^5.1.0 , you will get the warning: The argument type 'Null' can't be assigned to the parameter type 'QueryOptions<dynamic>'

I solved it by creating the mocked QueryOptions as:

class SutQueryOption extends Mock implements QueryOptions {}

void main() {
SutQueryOption _mockedQueryOption;
....

setUp(() {

SutQueryOption _mockedQueryOption = MockedQueryOptions();
....

});

when(mockClient.query(_mockedQueryOption)).thenAnswer((_) async => ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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