felangel / mocktail

A mock library for Dart inspired by mockito
https://pub.dev/packages/mocktail
MIT License
617 stars 81 forks source link

Incompatibility when specifying a data type to a variable that receives a mocked response in mocktail 0.3.0 #143

Closed JErazo7 closed 2 years ago

JErazo7 commented 2 years ago

Describe the bug

If a data type is specified to a variable that receives the mocked response, an exception is thrown with the following message: type 'Null' is not a subtype of type .....

Example:

when(() => graphQLClient.watchQuery(any()),
).thenReturn(MockObservableQuery());

This works:

final _rewardsMembershipQuery = graphQLClient.watchQuery();

This does not work:

ObservableQuery _rewardsMembershipQuery = graphQLClient.watchQuery();

To Reproduce Steps to reproduce the behavior:

  1. Clone this example repository: https://github.com/JErazo7/test_mocktail
  2. Run the test in test/mocktail_test.dart
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:graphql/client.dart';
import 'package:mocktail/mocktail.dart';

import 'rewards_membership_summary_query.ast.gql.dart'
    as rewards_membership_summary_query;

class SomeCubit extends Cubit<int> {
  final GraphQLClient client;
  late final ObservableQuery _rewardsMembershipQuery;

  SomeCubit(this.client) : super(0);

  void doSomething() {
    _rewardsMembershipQuery = client.watchQuery(
      WatchQueryOptions(
        operationName: 'RewardsMembershipSummary',
        document: rewards_membership_summary_query.document,
        fetchPolicy: FetchPolicy.noCache,
        fetchResults: true,
      ),
    );

    emit(1);
  }
}

class MockGraphQLClient extends Mock implements GraphQLClient {}

class MockObservableQuery extends Mock implements ObservableQuery {}

class MockWatchQueryOptions extends Mock implements WatchQueryOptions {}

void main() {
  late MockGraphQLClient graphQLClient;
  final rewardsObservableQuery = MockObservableQuery();

  setUp(() {
    graphQLClient = MockGraphQLClient();
    registerFallbackValue(MockWatchQueryOptions());
  });

  blocTest<SomeCubit, int>(
    'initial state is RewardsLoadInProgress',
    build: () {
      when(
        () => graphQLClient.watchQuery(any()),
      ).thenReturn(rewardsObservableQuery);
      return SomeCubit(graphQLClient);
    },
    act: (cubit) => cubit.doSomething(),
    expect: () => const [1],
  );
}
  1. See error
  2. Change line 12 of the above file from this
    late final ObservableQuery _rewardsMembershipQuery;

    to this:

    late final _rewardsMembershipQuery;
  3. Now it works.

Additional context

With in mocktail 0.2.0 works perfect. This is happen with the ObservableQuery data type of GraphQLClient.