How can I do this in Flutter Testing

Issue

I’m new to Flutter testing and was hoping If you could help me. I’ve a class called AddShimmer that looks like this ->

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. 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 ??

Solution

Basically flutter needs some additional context in order to render the widget. I usually just wrap it in a Scaffold inside a MaterialApp.

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

Answered By – mmcdon20

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.