在小部件测试中,如何执行 "pull-down" 手势?
In widget tests, how can I perform a "pull-down" gesture?
我正在使用 RefreshIndicator
小部件 - 用法如下所示:
RefreshIndicator(
onRefresh: () => refreshState(), // Invokes a method on a state object that is mocked in my test
child: ...
现在我想验证一下,“下拉”手势会导致调用 refreshState
:
testWidgets('Pulling down triggers refresh', (tester) async {
var state = MockState(); // Class extending Mock from mocktail
...
// pump widget, providing state as a ChangeNotifierProvider
...
// SomeWidget is a child in the widget under test and effective child of RefreshIndicator, it's center is positioned towards the top of the screen
var gesture = await tester.startGesture(tester.getCenter(find.byType(SomeWidget)));
await gesture.moveBy(Offset(0, 750));
await tester.pump();
verify(() => state.refresh());
});
不幸的是,这会导致测试失败,表明未调用该方法:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
No matching calls (actually, no calls at all).
(If you called `verify(...).called(0);`, please instead use `verifyNever(...);`.)
编辑:
按照 Michael 的建议,使用 tester.drag
和 tester.pumpAndSettle
而不是 tester.pump
解决了问题
您可以使用flutter_test中的拖动方法。
API 文档在这里:https://api.flutter.dev/flutter/flutter_test/WidgetController/drag.html
用法示例:
await tester.drag(find.byKey(ValueKey("Your Key")), Offset(0, 500));
我正在使用 RefreshIndicator
小部件 - 用法如下所示:
RefreshIndicator(
onRefresh: () => refreshState(), // Invokes a method on a state object that is mocked in my test
child: ...
现在我想验证一下,“下拉”手势会导致调用 refreshState
:
testWidgets('Pulling down triggers refresh', (tester) async {
var state = MockState(); // Class extending Mock from mocktail
...
// pump widget, providing state as a ChangeNotifierProvider
...
// SomeWidget is a child in the widget under test and effective child of RefreshIndicator, it's center is positioned towards the top of the screen
var gesture = await tester.startGesture(tester.getCenter(find.byType(SomeWidget)));
await gesture.moveBy(Offset(0, 750));
await tester.pump();
verify(() => state.refresh());
});
不幸的是,这会导致测试失败,表明未调用该方法:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
No matching calls (actually, no calls at all).
(If you called `verify(...).called(0);`, please instead use `verifyNever(...);`.)
编辑:
按照 Michael 的建议,使用 tester.drag
和 tester.pumpAndSettle
而不是 tester.pump
解决了问题
您可以使用flutter_test中的拖动方法。
API 文档在这里:https://api.flutter.dev/flutter/flutter_test/WidgetController/drag.html
用法示例:
await tester.drag(find.byKey(ValueKey("Your Key")), Offset(0, 500));