Mar*_*ary 5 testing stack flutter
假设我有一堆不一样的小部件:
return Stack(
children: <Widget>[
Container(),
Text('Hey'),
Positioned(top: 300.0, child: CustomWidget()),
],
);
Run Code Online (Sandbox Code Playgroud)
如何测试子部件的顺序?我可以为每个项目分配键,但如何判断哪个项目出现在另一个项目的前面?
我可以为我的 Stack 分配一个键,将每个子项包装在 中Positioned,然后使用find.byKey(stackKey)来获取我的 Stack,然后使用find.byType(Positioned)来获取它的子项。这会返回一个Iterable我可以转换为a的List. 但是,能find.byType()保证每次返回相同的订单吗?
我在这里所做的是将键添加到堆栈上的子项中,并包含预期顺序的编号。
\nStack(\n children: [\n Container(\n key: Key(\'StackChildKey1\'),\n ),\n Container(\n key: Key(\'StackChildKey2\'),\n ),\n Container(\n key: Key(\'StackChildKey3\'),\n ),\n ],\n),\nRun Code Online (Sandbox Code Playgroud)\n在测试脚本上,将所有匹配的键添加到我们稍后可以检查的列表中。
\nList<String> widgetOrderList = [];\ntester.allWidgets.forEach((Widget element) {\n /// Only add the Widget Key with the expected tag \'StackChildKey\'\n if (element.key.toString().contains(\'StackChildKey\')) {\n widgetOrderList.add(element.key.toString());\n }\n});\nRun Code Online (Sandbox Code Playgroud)\n用于String.compareTo(String)验证小部件的顺序。
// This List<int> contains String.compareTo(String) results\n/// https://api.flutter.dev/flutter/dart-core/String/compareTo.html\n/// 0 \xe2\x88\x92 when the Strings are equal.\n/// 1 \xe2\x88\x92 when the first String is greater than the second\n/// -1 \xe2\x88\x92 when the first String is smaller than the second\nList<int> sortCheck= [];\nfor (int i = 0; i < widgetOrderList.length; i++) {\n /// Compare current Widget Key with next while still within bounds\n if (i + 1 < widgetOrderList.length) {\n sortCheck.add(widgetOrderList[i].compareTo(widgetOrderList[i+1]));\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n然后用于expect添加测试用例。这期望结果为isWidgetStackSorted真。
/// Flutter test expects that the [isWidgetStackSorted] to be true\n/// if List<int> sortCheck contains either 0 or 1, this indicates\n/// that a pair isn\'t in the correct order on Stack\nvar isWidgetStackSorted = !(sortCheck.contains(0) || sortCheck.contains(1));\nexpect(isWidgetStackSorted, true);\nRun Code Online (Sandbox Code Playgroud)\n完成测试。
\n/// Flutter test expects that the [isWidgetStackSorted] to be true\n/// if List<int> sortCheck contains either 0 or 1, this indicates\n/// that a pair isn\'t in the correct order on Stack\nvar isWidgetStackSorted = !(sortCheck.contains(0) || sortCheck.contains(1));\nexpect(isWidgetStackSorted, true);\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2272 次 |
| 最近记录: |