扑来 - 如何在小部件测试中测试特定的lottie动画

发布于 2025-02-03 19:36:03 字数 1688 浏览 2 评论 0原文

嗨,我有一个Lottiebuilder Wichh通过资产的JSON加载动画。

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

我想编写一个测试以确定Bieng的特定JSON动画,所以我写了这样的测试:

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);
  });

我制作了一个假资产构建器,并将我的家用小部件包裹在它上:

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;
  }
}

我的主要功能看起来像这样:

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);
  });
}

仍然是测试 : t通过,任何帮助都将得到很大的批准。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文