2

我的自定义小部件代码:

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

列表模型代码:

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)

其中:表示没有找到,但应该有一个

我如何点击特定的列表项并验证它包含的项目是小部件有还是没有?我还想验证列表项计数?但它甚至找不到简单的文本

4

2 回答 2

2

我遇到过同样的问题。我在列表视图中有我的自定义小部件,需要在列表视图的子视图中测试小部件。

一开始我打电话:

debugDumpApp()

查看小部件是否存在于小部件树中。(它确实存在)

后来发现需要在ListView中设置shrinkWrap为true 。它奏效了

于 2020-04-22T14:14:15.823 回答
0

你可以这样试试:

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

接着

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

于 2020-06-10T15:10:48.753 回答