11

I created the following Flutter test for my widget

testWidgets("", (WidgetTester tester) async {
      await tester.pumpWidget(
        ChangeNotifierProvider<SettingsViewProvider>(
          create: (context) => SettingsViewProvider(),
          child: MaterialApp(
            localizationsDelegates: [S.delegate],
            home: SettingsScreen(),
          ),
        ),
      );

      final textFormFieldFinder = find.byElementType(TextFormField);
      await tester.pump();
      expect(textFormFieldFinder, findsNWidgets(3));
    });

The widget is a stateful widget which uses a ChangeNotifierProvider and the Consumer surrounds a List of three "TextFormFields".

Consumer<State>(
   builder: (context, value, child)=> Column(
       children: [TextFormField(...), TextFormField(...), TextFormField(...)];
   )
)

Expected: exactly 3 matching nodes in the widget tree

Actual: _ElementTypeFinder:

Which: means none were found but some were expected

Unfortunatly I receive that no widget is found in the widget tree.

Max Weber
  • 1,081
  • 11
  • 21
  • 1
    The problem is likely to be somewhere between your Consumer and your Provider. Try executing `debugDumpApp` to see what happens – Rémi Rousselet May 01 '20 at 11:27
  • I added the `debugDumpApp` the result contains the three TextFormFields Line 117 - Provider Line 121 - TextFormField No.1 Line 165 - TextFormField No.2 Line 213 - TextFormField No.3 https://1drv.ms/t/s!AvXUDvgUDO2xiodjesQ8j8kwB_LQzQ?e=vr0GHS – Max Weber May 01 '20 at 13:38

2 Answers2

13

As pointed out before, you should be using find.byType which is aimed at seaching Widgets rather than find.byElementType which deals with Elements.

Flutter has 3 UI building blocks:

Widgets (immutable) -> Elements (mutable) -> Render Objects -- they are not inherited from one another, those are separate types of objects with different purposes.

TextFormField is a Widget, while it is being passed to find.byElementType which expects an ancestor of Element

Maxim Saplin
  • 4,115
  • 38
  • 29
8

it seems to be solved using:

final textFormFieldFinder = find.byType(TextFormField);

instead of:

final textFormFieldFinder = find.byElementType(TextFormField);
camillo777
  • 2,177
  • 1
  • 20
  • 20