flutter_svg
flutter_svg copied to clipboard
How to test?
Hello!!!
I am developing an app that uses SvgPicture.network but I'm having a hard time trying to properly write some widget tests.
Anyone gots any advice?
Wold like to share a way of testing that I've found:
just add HttpOverrides.global = null; to your setUpAll of your tests.
By default, no network calls are allowed in tests. You can overwrite this behaviour like this:
HttpOverrides.global = _EnableHttpOverrides();
class _EnableHttpOverrides extends HttpOverrides {}
I had the same issue, leaving my solution here in case it helps you too.
@shinayser What you can do instead is have a switch to see if you are in a test and simply render a Container instead.
if (Platform.environment.containsKey('FLUTTER_TEST')) {
return Container(color: Colors.red);
}
return SvgPicture.network(...
Or as mentioned above, enable the network calls
Do not write unit tests that actually fetch from the network, they will be flaky :)
(Instead, mock out the HTTP client and just provide some asset)