【问题标题】:How to simulate onDoubleTap in flutter test如何在颤振测试中模拟 onDoubleTap
【发布时间】:2021-07-22 11:32:58
【问题描述】:

我正在尝试编写颤振测试并模拟双标签。但我找不到办法。

这是我现在所做的:

void main() {
  testWidgets('It should trigger onDoubleTap', (tester) async {
    await tester.pumpWidget(MaterialApp(
      home: GestureDetector(
        child: const Text('button'),
        onDoubleTap: () {
          print('double tapped');
        },
      ),
    ));

    await tester.pumpAndSettle();
    await tester.tap(find.text('button')); // <- Tried with tester.press too
    await tester.tap(find.text('button')); // <- Tried with tester.press too
    await tester.pumpAndSettle();
  });
}

当我运行测试时,这是我得到的:

00:03 +1: All tests passed!                                                                                              

但我在控制台中没有看到任何double tapped


如何触发双击?

【问题讨论】:

    标签: flutter dart flutter-test gesturedetector single-vs-double-tap


    【解决方案1】:

    解决方案是在两次点击之间等待kDoubleTapMinTime

    void main() {
      testWidgets('It should trigger onDoubleTap', (tester) async {
        await tester.pumpWidget(MaterialApp(
          home: GestureDetector(
            child: const Text('button'),
            onDoubleTap: () {
              print('double tapped');
            },
          ),
        ));
    
        await tester.pumpAndSettle();
        await tester.tap(find.text('button'));
        await tester.pump(kDoubleTapMinTime); // <- Add this
        await tester.tap(find.text('button'));
        await tester.pumpAndSettle();
      });
    }
    

    我在控制台中得到double tapped

    double tapped
    00:03 +1: All tests passed!
    

    【讨论】:

      猜你喜欢
      • 2021-05-21
      • 2021-09-25
      • 2022-08-11
      • 2019-06-13
      • 2019-08-19
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多