flutter_svg icon indicating copy to clipboard operation
flutter_svg copied to clipboard

How to test?

Open shinayser opened this issue 3 years ago • 4 comments
trafficstars

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?

shinayser avatar Jul 11 '22 21:07 shinayser

Wold like to share a way of testing that I've found:

just add HttpOverrides.global = null; to your setUpAll of your tests.

shinayser avatar Jul 14 '22 20:07 shinayser

By default, no network calls are allowed in tests. You can overwrite this behaviour like this:

HttpOverrides.global = _EnableHttpOverrides();

class _EnableHttpOverrides extends HttpOverrides {}

MuTe33 avatar Jul 16 '22 09:07 MuTe33

I had the same issue, leaving my solution here in case it helps you too.

Momy93 avatar Jul 30 '22 14:07 Momy93

@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

ikbendewilliam avatar Aug 27 '22 18:08 ikbendewilliam

Do not write unit tests that actually fetch from the network, they will be flaky :)

dnfield avatar Nov 18 '22 23:11 dnfield

(Instead, mock out the HTTP client and just provide some asset)

dnfield avatar Nov 18 '22 23:11 dnfield