flutter - 如何为 cupertino 数据选择器运行 flutter widget 测试?

标签 flutter flutter-test

我正在尝试为库比蒂诺日期选择器编写测试,但我找不到任何结果。如何为小部件测试选择特定的日期时间。

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/

相关文章:

ios - Firebase 未正确初始化

ios - 在 Flutter 中使用 ListWheelScrollView 时获取与图像相关的异常

flutter - 如何在 flutter 中将页脚添加到 ReorderableListView

flutter - 窗口小部件功能在列窗口小部件中不起作用?

flutter - 如何覆盖 Riverpod StateNotifier 的状态以进行测试

flutter 错误 : Could not download bcprov-jdk15on. jar (org.bouncycaSTLe :bcprov-jdk15on:1. 56)

flutter - 带有两行标签/文本的文本按钮图标,可能吗?

Flutter Google Charts 更改 TextStyleSpec 的 fontWeight

flutter - 如何在 Flutter 中启动外部应用程序(如 Skype)

flutter - 如何在 dart 中将这些代码块转换为不可为空的语句?