Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make cached_network_image friendly with widget testing #814

Open
darwin-morocho opened this issue Jan 6, 2023 · 1 comment
Open

Make cached_network_image friendly with widget testing #814

darwin-morocho opened this issue Jan 6, 2023 · 1 comment

Comments

@darwin-morocho
Copy link

darwin-morocho commented Jan 6, 2023

馃彈 Enhancement Proposal

Currently widget testing for CachedNetworkImage is very difficult and there are some packages for that like https://pub.dev/packages/network_image_mock but I think that could be more easy.

Pitch

class DefaultCacheManager extends CacheManager with ImageCacheManager {
  static const key = 'libCachedImageData';

  static DefaultCacheManager? _instance;

  static DefaultCacheManager? _mockCacheManager;

  factory DefaultCacheManager() {
    _instance ??= _mockCacheManager ?? DefaultCacheManager._();
    return _instance!;
  }

  DefaultCacheManager._() : super(Config(key));
}

@visibleForTesting
Future<void> mockDefaultCacheManager(
  Future Function() cb,
  DefaultCacheManager mockCacheManager,
) {
  DefaultCacheManager._mockCacheManager = mockCacheManager;
  await cb();
  DefaultCacheManager._mockCacheManager = null;
  DefaultCacheManager._instance = null;
}

then in our tests we can use

testWidgets('my test', 
   (tester) async {
    final mock = ....;
    return mockDefaultCacheManager(
      () async {
        /// OUR TEST CODE
      }
    );
    },
);
@meg4cyberc4t
Copy link

Very necessary for me.
There is no way to make a MockCacheManager and all goldens tests fall with an error.
Let's open the BaseCacheManager for export, or will we make an implementation for testing specifically and open it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants