简体   繁体   English

集成测试中的 ListView (Flutter)

[英]ListView in integration test (Flutter)

I haven't found a complex examples for testing ListViews.我还没有找到用于测试 ListViews 的复杂示例。 Example.例子。

I have a ListView, which has three objects Person .我有一个 ListView,它有三个对象Person

Person{
   String name;
   String surname;
}

In UI, Person is wrapped in Column , giving them two Text Widgets for name and surname .在 UI 中, Person被包裹在Column中,为他们提供了namesurname两个Text Widget。

I click on FAB.我点击FAB。 Clicking on FAB adds new Person with name Tom and surname Thomson to the ListView .单击 FAB 将name Tom和姓氏Thomson的新Person添加到ListView Now there are 4 objects of Person .现在有 4 个Person对象。 And I know data of the last one.我知道最后一个的数据。

How can I validate, that item was successfully added?如何验证该项目已成功添加? Is there a way to check length/count of ListView ?有没有办法检查ListViewlength/count Is there a way to validate last added item params?有没有办法验证最后添加的项目参数?

Appreciate!欣赏!

I add my ListTile titles with a unique key (ex. 'tile_surname_#') .我使用唯一键(ex. 'tile_surname_#')添加我的 ListTile 标题。 Assuming your ListView is built with an array of Person objects called persons, you can try something like:假设您的 ListView 是使用名为 Person 的 Person 对象数组构建的,您可以尝试以下操作:

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

widgetList provides a matching widget in the tree, just need to mention key of Listview which required to test. widgetList在树中提供了一个匹配的小部件,只需要提到需要测试的Listview的 key。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM