flutter - Flutter Listview Widget 测试失败,在列表中找不到错误文本?
问题描述
我的自定义小部件代码:
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)
其中:表示没有找到,但应该有一个
我如何点击特定的列表项并验证它包含的项目是小部件有还是没有?我还想验证列表项计数?但它甚至找不到简单的文本
解决方案
我遇到过同样的问题。我在列表视图中有我的自定义小部件,需要在列表视图的子视图中测试小部件。
一开始我打电话:
debugDumpApp()
查看小部件是否存在于小部件树中。(它确实存在)
后来发现需要在ListView中设置shrinkWrap为true 。它奏效了
推荐阅读
- mysql - 选择数据“不存在”并选择为空或“为空”的列 - MySQL
- javascript - 如何在 Cordova 中使用 Electron 平台执行 Bash 命令
- chai - 在柏树中,因为()函数在哪里?
- python - Model.fit_generator 抛出运行时错误:
- alexa - 如何在 Alexa 技能中创建复合插槽类型?
- django - Django CMS (3.6.0) 自动语言切换/检测未按预期工作
- sql - 使用带有实体框架的 .SqlQuery 处理来自存储过程的多个结果集
- c - 如何读入文件,然后将文件中的每个结构放入数组中?
- reactjs - 反应选择不适用于反应传单弹出
- java - 创建数据矩阵时如何使用 pantone 代码?