'Flutter widget testing how to mock a network image
I am trying to test my widgets, but I keep getting the 400 error on my network images. This is expected behavior because flutter doesn't want you to make http requests in a test. But because of the behaviour my widget tests fail. They have a image.network(url) in them.
I have tried with the image_test_utils package but discoverd quikly that this package is not maintained sinds 2018. So I came on the Network Image Mock package. This is maintained but isn't working: (Not my issue, but the same) https://github.com/stelynx/network_image_mock/issues/6
Now I want to try with something flutter made themselves: I added this: https://github.com/flutter/flutter_markdown/blob/a2fa53b7a29dc58b6f26b53e23a6f5a8d283dd66/test/flutter_markdown_test.dart#L506-L548
But I have no clue on how to use it.
Solution 1:[1]
so sorry for the late answer, so i found this dependency that works perfectly with mockito.
pubspec.yaml:
dev_dependencies:
network_image_mock: ^2.0.1
test.dart:
mockNetworkImagesFor(
() async {
return tester.pumpWidget(
MaterialApp(
theme: lightTheme,
home: Container(
alignment: Alignment.center,
width: getScreenWidth(),
height: getScreenHeight(),
child: //widget here
),
),
);
},
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | A -Z |
