testing - 如何在 Flutter 中测试时找到 Widget 的 `text` 属性?
问题描述
我有一段代码可以创建一个文本表小部件,如下所示:
return Table(
defaultColumnWidth: FixedColumnWidth(120.0),
children: <TableRow>[
TableRow(
children: <Widget>[Text('toffee'), Text('potato')],
),
TableRow(
children: <Widget>[Text('cheese'), Text('pie')],
),
],
);
我想测试表中的第一项确实是“太妃糖”这个词。我设置了我的测试并进入这部分:
var firstCell = find
.descendant(
of: find.byType(Table),
matching: find.byType(Text),
)
.evaluate()
.toList()[0].widget;
expect(firstCell, 'toffee');
这绝对行不通,因为firstCell
它是 Widget 类型,它不等于 String toffee
。
我只看到一个toString()
函数,如下所示:
'Text("toffee", inherit: true, color: Color(0xff616161), size: 16.0,
textAlign: left)'
如何提取text
属性以获取单词toffee
?
现在看来我所能做的就是检查.toString().contains('toffee')
哪个不理想。
解决方案
您可以将您firstCell
的投到Text
.
var firstCell = find
.descendant(
of: find.byType(Table),
matching: find.byType(Text),
)
.evaluate()
.whereType<Text>()
.first;
然后测试firstCell.data
expect(firstCell.data, 'toffee');
推荐阅读
- sql - SQL Server:时间的十进制差异
- ios - 如何等到 SKAction.repeatForever 完成 SpriteKit 中的动画序列?
- sql - 用另一列的数据更新列
- mysql - 更新和自我加入和案例陈述 - 哦,我的
- kubernetes - 字段 'instance.networkInterfaces[0].accessConfigs[0].natIP' 的值无效:指定的外部 IP 地址未在区域中保留
- assembly - 带有字节的标记的平均值
- c++ - 改变整个屏幕像素的颜色
- intellij-idea - IntelliJ 无法解析捆绑包中的 Kotlin stdlib 成员
- linux - 配置命令错误(不支持arch)
- java - 在捕获异常时做一些事情