Getting type 'Null' is not a subtype of type 'Future<Info>' in type cast in flutter unit testing using mockito

Viewed 173

I have generated mock class for my store and repository class using @GenerateMocks as below::

@GenerateMocks([],customMocks: [

  MockSpec<MyStore>(as: #MockMyStore, returnNullOnMissingStub: true),

  MockSpec<MyRepository>(as: #MockMyRepository, returnNullOnMissingStub: true),

])

this has automatically created a test.mocks file for both store and repository class but while writing unit testing I am getting error: 'Null' is not a subtype of type 'Future' in type cast.

The test cases I have written as below:

test('call api successfully', () async {

      when(mockStore.calApi()).thenAnswer((realInvocation) => Future.value());
      expect(await store.calApi(), true);
    });

//MyStore class

class MyStore = _MyStore with _$MyStore;

abstract class _MyStore with Store {
  late MyRepository _repository;

  _MyStore(MyStore repository)
      : this._repository = repository;
Future<bool> callApi() async {
    
        final isInternet = await Util.checkInternetAndShowDialog(context);
    
        if (isInternet) {
    
          final future = _repository.callApi();
    
          var res = await future;
    
          return true;
    
        }
    
    return false;
    
        }
}

Can any one help me to resolve this issue, why I am receiving 'Null' subtype error and pass the test cases in flutter. I shall be thankful for this.

0 Answers
Related