Flutter Listview Widget测试失败,并在列表内找不到错误文本?

时间:2019-05-15 20:26:57

标签: flutter flutter-test flutter-listview

我的自定义小部件代码:

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

使用自定义窗口小部件的我的应用程序

    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代码:

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

我尝试过的小部件测试代码

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

错误消息:

    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)

其中:表示未找到任何东西,但预期会有一个

我如何点击特定的列表项,并验证该列表项是否包含小部件所具有的内容? 我还想验证列表项的数量吗? 但它甚至找不到简单的文字

2 个答案:

答案 0 :(得分:1)

我有同样的问题。我将自定义窗口小部件放在listview中,并且需要在listview的子级中测试该窗口小部件。

一开始我打电话给:

debugDumpApp()

查看该窗口小部件树中是否存在该窗口小部件。 (并且确实存在)

后来我发现您需要在ListView中将 shrinkWrap设置为true 。而且有效

答案 1 :(得分:0)

您可以这样尝试吗?

ListModel( 键:键(“ Inducesmile”), 文字:“ Inducesmile.com”, 指数:1 ),

然后

expect(find.byKey( Key(“ Inducesmile” )),findsOneWidget);