Flutter Listview Widget test fails with error text not found inside list?

佐手、 提交于 2020-06-13 05:59:31

问题


My custom widget code:

    class CustomWidget extends StatefulWidget {

      final int index;
      final String value;
      final bool isSelected;
      final VoidCallback onSelect;

      const CustomWidget({
            Key key,
    @required this.index,
    @required this.value,
    @required this.isSelected,
    @required this.onSelect,
  })  : assert(index != null),
        assert(value != null),
        assert(isSelected != null),
        assert(onSelect != null),
        super(key: key);

      @override
      _CustomWidgetState createState() => _CustomWidgetState();
    }

    class _CustomWidgetState extends State<CustomWidget> {
      @override
      Widget build(BuildContext context) {
        return GestureDetector(
          onTap: widget.onSelect,
          child: Container(
            margin: EdgeInsets.all(5.0),
            child: ListTile(
               title: Text(widget.index == 1 ? " ${widget.value} ${widget.index}" : "${widget.value}" ),
            ),
            decoration: widget.isSelected
                ? BoxDecoration(color: Colors.black38, border: Border.all(color: Colors.black))
                : BoxDecoration(),
          ),
        );
      }
    }

My Application using custom widget

    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }

    class _MyHomePageState extends State<MyHomePage> {
      int currentSelectedIndex;
 List<ListModel> _list = [
    ListModel(
      text: "Flutter.dev",
      index: 0,
    ),
    ListModel(
      text: "Inducesmile.com",
      index: 1,
    ),
    ListModel(
      text: "Google.com",
      index: 2,
    ),
    ListModel(
      text: "Yahoo.com",
      index: 3,
    ),
  ];

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Selected index is $currentSelectedIndex'),
          ),
          body: ListView.builder(
            itemCount: _list.length,
            itemBuilder: (context, index) {
              return CustomWidget(
                index: index,
                value: _list[index].text,
                isSelected: currentSelectedIndex == index,
                onSelect: () {
                  setState(() {
                    currentSelectedIndex = index;
                  });
                },
              );
            },
          ),
        );
      }
    }

ListModel code:

class ListModel {
  String text;
  int index;
  ListModel({this.text, this.index});
}

Widget test code i tried

 testWidgets("Find text", (WidgetTester tester) async {
    final testableWidget = MyApp();
    await tester.pumpWidget(testableWidget);
    expect(find.widgetWithText(CustomWidget,"Inducesmile.com 1"), findsOneWidget);
  });

An Error message:

    Error: The following TestFailure object was thrown running a test:
      Expected: exactly one matching node in the widget tree
      Actual: ?:<zero widgets with type "CustomWidget" which is an ancestor of text "Inducesmile.com 1">
When the exception was thrown, this was the stack:
#4      main.<anonymous closure> (file:///Users/testUser/Documents/my_app/test/widget_test.dart:23:5)
<asynchronous suspension>
#5      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:82:23)
#6      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:571:19)
<asynchronous suspension>
#9      TestWidgetsFlutterBinding._runTest (package:flutter_test/src/binding.dart:555:14)

Which: means none were found but one was expected

How can i tap the particular list item and verify it out that item it contains is what the widget has or not ? I also want to verify list items count? But it even not find the simple text


回答1:


I had the same issue. I had my custom widget inside listview and needed to test the widget inside the children of the listview.

At the beginning I called:

debugDumpApp()

to see if the widget exists in the widget tree. (and it did exist)

Later I found that you need to set shrinkWrap to true in ListView. and It worked




回答2:


Can you try like this:

ListModel( key: Key("Inducesmile"), text: "Inducesmile.com", index: 1, ),

And then

expect(find.byKey(Key("Inducesmile")), findsOneWidget);



来源:https://stackoverflow.com/questions/56157140/flutter-listview-widget-test-fails-with-error-text-not-found-inside-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!