我怎样才能在 Flutter 测试中做到这一点
How can I do this in Flutter Testing
我是 Flutter 测试的新手,希望您能帮助我。我有一个名为 AddShimmer 的 class,看起来像这样 ->
AddShimmer(
child: ListTile(
title: buildLoadingAnimation(context),
),
),
现在,我正在尝试编写一个 flutter 测试,以检查是否将子部件传递给 AddShimmer class。为此,我正在写这样的东西 ->
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
这是什么意思以及如何测试它??
基本上 flutter 需要一些额外的上下文才能呈现小部件。我通常只是将它包裹在 Scaffold
内 MaterialApp
.
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: AddShimmer(child: childWidget),
),
),
);
我是 Flutter 测试的新手,希望您能帮助我。我有一个名为 AddShimmer 的 class,看起来像这样 ->
AddShimmer(
child: ListTile(
title: buildLoadingAnimation(context),
),
),
现在,我正在尝试编写一个 flutter 测试,以检查是否将子部件传递给 AddShimmer class。为此,我正在写这样的东西 ->
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
这是什么意思以及如何测试它??
基本上 flutter 需要一些额外的上下文才能呈现小部件。我通常只是将它包裹在 Scaffold
内 MaterialApp
.
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: AddShimmer(child: childWidget),
),
),
);