dnfield / flutter_svg

SVG parsing, rendering, and widget library for Flutter
MIT License
1.66k stars 454 forks source link

Using flutter_svg in tests #738

Open rgjurgens opened 2 years ago

rgjurgens commented 2 years ago

I'm using flutter_svg throughout our project and it works just fine. However, if I'm running the app in an integration test, the app crashes on assets that cannot be found. The error message is as follows, I'm using the latest flutter_svg version:

══╡ EXCEPTION CAUGHT BY SVG ╞═══════════════════════════════════════════════════════════════════════
The following assertion was thrown resolving a single-frame picture stream:
Unable to load asset: assets/buttons/close_button.svg

This is the code in my test:

import '../main.dart' as app;

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  group('end-to-end test', () {
    testWidgets('tap on the floating action button, verify counter',
        (tester) async {
      app.main();
      await tester.pumpAndSettle();
rgjurgens commented 2 years ago

Any thoughts, remarks? Do I need to provide more information?

Momy93 commented 2 years ago

I had the same issue SvgPicture.network, and I solved with HttpOverrides.

In your case, you could mock the AssetBundle.

Another solution that I used in my code, is to convert the svg icons to a font pack. That way I can use them as Icon like I would do with a material icon, instead of Svg.

HelenaSewell commented 1 year ago

Realise this was a long time ago, but what folders do you have added to your pubspec.yaml? I saw this issue when I forgot to add the path to the containing folder, as well as the overall folder. ie

assets:
  - assets/
  - assets/buttons/

How the assets loaded was actually not consistent between running the app and running the tests. Adding the subfolders made it work for both.