简体   繁体   English

如何在 Flutter 测试中做到这一点

[英]How can I do this in Flutter Testing

I'm new to Flutter testing and was hoping If you could help me.我是 Flutter 测试的新手,希望你能帮助我。 I've a class called AddShimmer that looks like this ->我有一个名为 AddShimmer 的 class,看起来像这样 ->

AddShimmer(
          child: ListTile(
            title: buildLoadingAnimation(context),
          ),
        ),

Now, I'm trying to write a flutter test just to check if a child widget is being passed to the AddShimmer class.现在,我正在尝试编写 flutter 测试,以检查是否将子小部件传递给 AddShimmer class。 To do that, I'm writing something like this ->为此,我正在写这样的东西->

testWidgets('Shimmer has a child widget', (WidgetTester tester) async {
    const childWidget = ListTile(
      title: Text('Loading'),
    );
    await tester.pumpWidget(
     const AddShimmer(child: childWidget)
    );
    expect(find.byWidget(childWidget), findsOneWidget);
  });

While I'm executing the test, I'm getting an exception which says ->当我执行测试时,我得到一个异常,上面写着 ->

The following assertion was thrown building ListTile(dirty):
No Directionality widget found.
ListTile widgets require a Directionality widget ancestor.
The specific widget that could not find a Directionality ancestor was:
  ListTile

What does it mean and how to test this out??这是什么意思以及如何测试呢?

Basically flutter needs some additional context in order to render the widget.基本上 flutter 需要一些额外的上下文来呈现小部件。 I usually just wrap it in a Scaffold inside a MaterialApp .我通常只是将它包装在MaterialApp内的Scaffold中。

await tester.pumpWidget(
  const MaterialApp(
    home: Scaffold(
      body: AddShimmer(child: childWidget),
    ),
  ),
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM