我正在尝试为库比蒂诺日期选择器编写测试,但我找不到任何结果。如何为小部件测试选择特定的日期时间。
testWidgets("description", (WidgetTester tester)async{
await tester.pumpWidget(MaterialApp(home: MyPage()));
final textField=find.byKey(Key("nameTextField"));
final datePicker=find.byKey(Key("birthDatePicker"));
final button=find.byType(ElevatedButton);
await tester.enterText(textField, "not important");
await tester.drag(datePicker, Offset(0.0, 10.0)); ///Here the problem ?
await tester.press(button);
await tester.pump();
expect(find.text("Error Text"), findsOneWidget);
});
我的问题是,如何选择 1/1/2021 进行小部件测试?
最佳答案
看一下为小部件实现的测试,这些测试结果是不言自明的。他们倾向于在整个文本中使用引用,并以恒定的偏移量进行移动。
https://github.com/flutter/flutter/blob/master/packages/flutter/test/cupertino/date_picker_test.dart
我还在下面给你留了一个例子
testWidgets('picker automatically scrolls away from invalid date on day change', (WidgetTester tester) async {
late DateTime date;
await tester.pumpWidget(
CupertinoApp(
home: Center(
child: SizedBox(
height: 400.0,
width: 400.0,
child: CupertinoDatePicker(
mode: CupertinoDatePickerMode.date,
onDateTimeChanged: (DateTime newDate) {
date = newDate;
},
initialDateTime: DateTime(2018, 2, 27), // 2018 has 28 days in Feb.
),
),
),
),
);
await tester.drag(find.text('27'), const Offset(0.0, -32.0), touchSlopY: 0.0, warnIfMissed: false); // see top of file
await tester.pump();
expect(
date,
DateTime(2018, 2, 28),
);
await tester.drag(find.text('28'), const Offset(0.0, -32.0), touchSlopY: 0.0, warnIfMissed: false); // see top of file
await tester.pump(); // Once to trigger the post frame animate call.
// Callback doesn't transiently go into invalid dates.
expect(
date,
DateTime(2018, 2, 28),
);
// Momentarily, the invalid 29th of Feb is dragged into the middle.
expect(
tester.getTopLeft(find.text('2018')).dy,
tester.getTopLeft(find.text('29')).dy,
);
await tester.pump(); // Once to start the DrivenScrollActivity.
await tester.pump(const Duration(milliseconds: 500));
expect(
date,
DateTime(2018, 2, 28),
);
expect(
tester.getTopLeft(find.text('2018')).dy,
tester.getTopLeft(find.text('28')).dy,
);
});
关于flutter - 如何为 cupertino 数据选择器运行 flutter widget 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68018004/