1

我有以下功能

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

我想验证在以下测试中是否调用了 cacheData 函数

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

所以我做了这样一个模拟

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

但它会抛出 Used on a non-mockito object 错误。我怎样才能实现它?

4

0 回答 0