1

I have following function

Future<T> execute<T>(Future<T> Function(ApiService apiService) getFromRemote,
      {Future<void> Function(T data) cacheData, Future<T> Function() getFromCache}) {
}

And I want to verify that cacheData function is called in following test

CacheDataFunction mockCacheDataFunction;
setUpAll(() {
  mockCacheDataFunction = CacheDataFunction();
});
test('caches value provided in future', () async {
  await repositoryRequestExecutor.execute<int>(
    (_) => Future.value(testValue),
    cacheData: (data) => mockCacheDataFunction<int>(data),
    getFromCache: () => Future.value(testValueFromCache),
  );
  verify(mockCacheDataFunction<int>(testValue));
});

So I made such a mock

class CacheDataFunction extends Mock{
  Future<void> call<T>(T data) => Future.value();
}

But it throws Used on a non-mockito object error. How can I achieve it?

Ilya Maximencko
  • 411
  • 2
  • 15
  • `Mock`s should always be used as `class CacheDataFunction extends Mock {}` with no implementation. Implementing methods in it is an anti-pattern since it undermines the ability for the `Mock` to do its usual things. – jamesdlin Nov 13 '20 at 21:30
  • @jamesdlin well I know, I found this solution on stackoverflow but it doesn't work. That is just what I tried to do – Ilya Maximencko Nov 14 '20 at 17:02

0 Answers0