gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:00:31 25 4
gpt4 key购买 nike

我的自定义小部件代码:

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

我试过的Widget测试代码

 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)

Which: 表示没有找到但预期有一个

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

最佳答案

我遇到了同样的问题。我在 ListView 中有我的自定义小部件,需要在 ListView 的子项中测试小部件。

一开始我调用:

debugDumpApp()

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

后来发现需要在ListView中设置shrinkWrap为true。并且有效

关于Flutter Listview Widget 测试失败并在列表中找不到错误文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56157140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com