Baseflow / Baseflow/flutter_cached_network_image

How to mock cache manager properly?

Open
#604 4 comments 3 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
2.6k
Forks
731
Avg merge
3d 9h
Merged PRs (30d)
1

Description

I used to use in my Golden tests a TestCacheManager from [this](https://medium.com/flutter-community/golden-testing-using-cachednetworkimage-1b488c653af3) article and it worked fine. After upgrading to null-safety I have no idea how to achieve the same behavior on the new version of the `flutter_cached_network_image`.

The problem is that I need to return the `FileInfo` object which is can be created only via `fileSystem.createFile(cacheObject.relativePath)` which is impossible in tests. I tried to use tests from the `flutter_cache_manager` library, but after copying almost 10 files (for just mocking the image!) it still returned me an error with the `getFile` method, where I can't use `any` parameter because it required non-nullable key (due to restrictions in Mockito after migrating to NNBD).

Thus, my question is:
How to achieve the same behavior on the latest version of the lib? I spent a few hours without success.

```
import 'dart:io';

import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_cache_manager/src/cache_store.dart';
import 'package:flutter_cache_manager/src/storage/non_storing_object_provider.dart';

class TestCacheManager extends BaseCacheManager {
TestCacheManager()
: super(
null,
cacheStore: CacheStore(
Future.value(null),
null,
null,
null,
cacheRepoProvider: Future.value(NonStoringObjectProvider()),
),
);

@override
Future getFilePath() async {
return null;
}

@override
Stream getFileStream(String url,
{Map headers, bool withProgress}) async* {
if (url == 'https://test.com/example') {
yield FileInfo(
File('test/assets/mock_image.png'),
FileSource.Cache,
DateTime(2050),
url,
);
}
}
}

```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.