【问题标题】:ListView in integration test (Flutter)集成测试中的 ListView (Flutter)
【发布时间】:2020-11-05 14:01:36
【问题描述】:

我还没有找到用于测试 ListViews 的复杂示例。示例。

我有一个 ListView,它有三个对象Person

Person{
   String name;
   String surname;
}

在 UI 中,Person 被包裹在 Column 中,为它们提供了两个 Text 小部件,分别用于 namesurname

我点击FAB。单击 FAB 将新的 Personname Tom 和姓氏 Thomson 添加到 ListView。现在有Person的4个对象。而且我知道最后一个的数据。

如何验证该项目已成功添加?有没有办法检查length/countListView? 有没有办法验证最后添加的项目参数?

欣赏!

【问题讨论】:

    标签: flutter listview testing driver integration


    【解决方案1】:

    我使用唯一键添加我的 ListTile 标题(例如'tile_surname_#')。假设您的 ListView 是使用名为 Person 的 Person 对象数组构建的,您可以尝试以下操作:

        final String valueKey = 'tile_$newPerson.surname_${persons.length}';
        final newPersonFinder = find.byValueKey(valueKey);
        expect(
           await driver.getText(newPersonFinder), valueKey
        );
    

    【讨论】:

    • 是的,我使用类似的模式。但这似乎非常手动,并且代码变得难以处理,钥匙无处不在。删除案例呢?现在有 4 个对象,person Tom 被删除。如何验证 Person Tom 不再存在于 List 中?因为在这种情况下getText 永远不会执行,因为对象不存在。
    • 试试waitForAbsent方法
    • 我不确定这是使用它的正确用例,因为waitForAbsent 用于您希望小部件消失的情况。在这种特殊情况下,小部件根本没有被定位。我希望会有某种expect 声明。
    • true,但如果您“等待 driver.waitForAbsent(deletedPerson)”并且此人未从 ListView 中删除,则测试将失败并出现超时异常。
    • 正确,我试图做的是将它包装起来尝试捕获,但它似乎不起作用。首先出现DriverError,然后出现TimeoutException,它通过了整个try catch 子句并且仍然没有通过测试。
    【解决方案2】:

    widgetList在树中提供了一个匹配的widget,只需要提到需要测试的Listview的key。

    final count = tester
              .widgetList<ListView>(find.byKey(ValueKey("ListViewKey")))
              .length;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-12
      • 2022-11-29
      • 2019-08-01
      • 2022-10-31
      • 2019-07-05
      • 2021-06-12
      • 2018-12-31
      相关资源
      最近更新 更多