1

Hi I have a LottieBuilder wichh loads an animation via json from assets.

LottieBuilder.asset(
            'assets/animations/no_wifi_animation.json',
            repeat: true),

i want to a write a test to determine that the specific json animation is bieng rendered so i wrote a test like this :

testWidgets('NotConnectedScreen LottieBuilder plays the correct Animation',
      (tester) async {
    final lottieBilder =
        LottieBuilder.asset('assets/animations/no_wifi_animation.json');
    await tester.pumpWidget(myApp);
    expect(find.byWidget(lottieBilder), findsOneWidget);
  });

i made a fake AssetBuilder and wrapped my home widget around it :

class FakeAssetsBundle extends Fake implements AssetBundle{
  final String animationJson = File('assets/animations/no_wifi_animation.json').readAsStringSync();

  @override
  Future<String> loadString(String key, {bool cache = true}) async{
    return animationJson;
  }
}

and my main function looks like this :

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();
  late Widget myApp;
  late FakeAssetsBundle fakeAssetsBundle;

  setUp(() {
    fakeAssetsBundle = FakeAssetsBundle();
    myApp = MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: DefaultAssetBundle(
        bundle: fakeAssetsBundle,
        child: const NotConnectedScreen()),
    );
  });
testWidgets('NotConnectedScreen LottieBuilder plays the correct Animation',
      (tester) async {
    final lottieBilder =
        LottieBuilder.asset('assets/animations/no_wifi_animation.json');
    await tester.pumpWidget(myApp);
    expect(find.byWidget(lottieBilder), findsOneWidget);
  });
}

still the test doesn't pass , any help would be much apprieciated.

R3HP
  • 470
  • 4
  • 13

0 Answers0