1

我是 Flutter 测试的新手,希望你能帮助我。我有一个名为 AddShimmer 的类,看起来像这样 ->

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

现在,我正在尝试编写一个颤振测试,只是为了检查一个子小部件是否正在传递给 AddShimmer 类。为此,我正在写这样的东西->

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

当我执行测试时,我得到一个异常,上面写着 ->

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

这是什么意思以及如何测试呢?

4

1 回答 1

1

基本上,flutter 需要一些额外的上下文来渲染小部件。我通常只是把它包在一个Scaffold里面MaterialApp

await tester.pumpWidget(
  const MaterialApp(
    home: Scaffold(
      body: AddShimmer(child: childWidget),
    ),
  ),
);
于 2021-12-20T04:45:37.320 回答