如何在Flutter测试期间找到Widget的`text`属性?

问题描述 投票:2回答:2

我有一段代码创建了一个Table of 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')

testing text dart flutter
2个回答
4
投票

你可以将你的firstCell施放到Text

var firstCell = find
    .descendant(
      of: find.byType(Table),
      matching: find.byType(Text),
    )
    .evaluate()
    .whereType<Text>()
    .first;

然后测试firstCell.data

expect(firstCell.data, 'toffee');

1
投票

按文字查找?

expect(find.text('toffee'), findsOneWidget);
© www.soinside.com 2019 - 2024. All rights reserved.