gpt4 book ai didi

testing - 在 Flutter 中测试期间如何找到堆栈的顺序?

转载 作者:行者123 更新时间:2023-12-04 13:59:28 25 4
gpt4 key购买 nike

假设我有一堆不一样的小部件:

return Stack(
children: <Widget>[
Container(),
Text('Hey'),
Positioned(top: 300.0, child: CustomWidget()),
],
);

如何测试子小部件的顺序?我可以为每个项目分配键,但我怎么知道哪个项目出现在另一个项目之前?

我可以为我的 Stack 分配一个键,将每个 child 包裹在 Positioned 中,然后使用 find.byKey(stackKey)获取我的堆栈,然后使用 find.byType(Positioned)得到它的 child 。这将返回 Iterable我可以将其转换为 List .但是,是 find.byType()保证每次都返回相同的订单?

最佳答案

我在这里所做的是将 Keys 添加到 Stack 上的子项中,并带有预期顺序的编号。

Stack(
children: [
Container(
key: Key('StackChildKey1'),
),
Container(
key: Key('StackChildKey2'),
),
Container(
key: Key('StackChildKey3'),
),
],
),
在测试脚本中,将所有匹配的键添加到我们可以稍后检查的列表中。
List<String> widgetOrderList = [];
tester.allWidgets.forEach((Widget element) {
/// Only add the Widget Key with the expected tag 'StackChildKey'
if (element.key.toString().contains('StackChildKey')) {
widgetOrderList.add(element.key.toString());
}
});
使用 String.compareTo(String)验证小部件的顺序。
// This List<int> contains String.compareTo(String) results
/// https://api.flutter.dev/flutter/dart-core/String/compareTo.html
/// 0 − when the Strings are equal.
/// 1 − when the first String is greater than the second
/// -1 − when the first String is smaller than the second
List<int> sortCheck= [];
for (int i = 0; i < widgetOrderList.length; i++) {
/// Compare current Widget Key with next while still within bounds
if (i + 1 < widgetOrderList.length) {
sortCheck.add(widgetOrderList[i].compareTo(widgetOrderList[i+1]));
}
}
然后使用 expect添加测试用例。这期待 isWidgetStackSorted 的结果是真实的。
/// Flutter test expects that the [isWidgetStackSorted] to be true
/// if List<int> sortCheck contains either 0 or 1, this indicates
/// that a pair isn't in the correct order on Stack
var isWidgetStackSorted = !(sortCheck.contains(0) || sortCheck.contains(1));
expect(isWidgetStackSorted, true);
完成测试。
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

testWidgets('Test Stack Order', (WidgetTester tester) async {
// Build the app and trigger a frame.
await tester.pumpWidget(MyApp());

List<String> widgetOrderList = [];
tester.allWidgets.forEach((Widget element) {
/// Only add the Widget Key with the expected tag 'StackChildKey'
if (element.key.toString().contains('StackChildKey')) {
widgetOrderList.add(element.key.toString());
}
});

/// This List<int> contains String.compareTo(String) results
/// https://api.flutter.dev/flutter/dart-core/String/compareTo.html
/// 0 − when the Strings are equal.
/// 1 − when the first String is greater than the second
/// -1 − when the first String is smaller than the second
List<int> sortCheck= [];
for (int i = 0; i < widgetOrderList.length; i++) {
/// Compare current Widget Key with next while still within bounds
if (i + 1 < widgetOrderList.length) {
sortCheck.add(widgetOrderList[i].compareTo(widgetOrderList[i+1]));
}
}

/// Flutter test expects that the [isWidgetStackSorted] to be true
/// if List<int> sortCheck contains either 0 or 1, this indicates
/// that a pair isn't in the correct order on Stack
var isWidgetStackSorted = !(sortCheck.contains(0) || sortCheck.contains(1));
expect(isWidgetStackSorted, true);
});
}

关于testing - 在 Flutter 中测试期间如何找到堆栈的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53511690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com