mockNetworkImages<T> function
Utility method that allows you to execute a widget test when you pump a
widget that contains an Image.network
node in its tree.
For example, if you have the following app:
class FakeApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Image.network(
'https://uploads-ssl.webflow.com/5ee12d8d7f840543bde883de/5eec278f49a4916759d679aa_vgv-wordmark-black.svg',
),
),
),
);
}
}
You can test it like this:
void main() {
testWidgets('can use mocktail for network images', (tester) async {
await mockNetworkImages(() async {
await tester.pumpWidget(FakeApp());
expect(find.byType(Image), findsOneWidget);
});
});
}
Implementation
T mockNetworkImages<T>(
T Function() body, {
Uint8List? imageBytes,
ImageResolver? imageResolver,
}) {
assert(
imageBytes == null || imageResolver == null,
'One of imageBytes or imageResolver can be provided, but not both.',
);
return HttpOverrides.runZoned(
body,
createHttpClient: (_) => _createHttpClient(
imageResolver ??= _defaultImageResolver(imageBytes),
),
);
}