According to the documentation, an http call during widgets test will fail with a 400 status code.
So why the code below never displays the errorWidget ? However it displays the progressWidget and gets stuck there.
testWidgets('Cached networked image 400', (tester) async {
final widget = CachedNetworkImage(
imageBuilder: (
context,
url,
) =>
Container(
key: Key('image'),
),
progressIndicatorBuilder: (context, url, progress) => Container(
key: Key('progress'),
),
errorWidget: (context, url, error) => Container(
key: Key('failed'),
),
imageUrl: 'https://example.com/image.png',
);
await tester.pumpWidget(
MediaQuery(
data: MediaQueryData(),
child: MaterialApp(
home: widget,
),
),
);
await tester.pumpAndSettle();
expect(find.byKey(Key('progress')), findsOneWidget); // this should be findsNothing but why does it pass here?
expect(find.byKey(Key('failed')), findsOneWidget);
expect(find.byKey(Key('image')), findsNothing);
I am getting:
Expected: exactly one matching node in the widget tree
Actual: _KeyFinder:] (ignoring offstage widgets)>
The plugin: https://pub.dev/packages/cached_network_image
Progress Widget should not display anymore
Image Widget should not display
Failed Widget should display
Just run the above code in test.
Version: 2.2.0+1
Platform:
FYI, I've traced the issue and found that it keeps in progress since it stuck on open method of cache object provider: https://github.com/Baseflow/flutter_cache_manager/blob/develop/flutter_cache_manager/lib/src/storage/cache_object_provider.dart#L15
I guess it is better to use a mock CacheManager, because than you really only test the image, and not the CacheManager as well.
Unable to load image. Loading spinner keep on spinning.
I guess bug reporter & commenters are looking for examples from community :)
Like this? https://medium.com/@ncuillery/golden-testing-using-cachednetworkimage-1b488c653af3
For now I'll close this issue as it looks like it is solved with this blog post. For improving testability there are other issues like https://github.com/Baseflow/flutter_cache_manager/issues/2
Most helpful comment
Like this? https://medium.com/@ncuillery/golden-testing-using-cachednetworkimage-1b488c653af3