当我试图测试我的Flutter应用程序时,遇到了一个错误。我有一个自定义的小部件,位于
我已经尝试使用
如何在测试中滚动到
这个最小化的复制品显示了包含
在这个迭代中,我使用了
CustomScrollView
小部件的底部(超出第一个视口)。在我的测试中,我想验证它实际上是否存在。我已经尝试使用
WidgetTester.scrollUntilVisible
以及WidgetTester.drag
(就像在flutter框架的测试中所做的那样)。此外,我还尝试使用FlutterDriver
重构我的测试,但这只是彻底搞砸了一切其他东西。 如何在测试中滚动到
CustomScrollView
的底部?这个最小化的复制品显示了包含
CustomScrollView
的应用程序,它具有高度为一个屏幕的容器小部件(以便我想要查找的小部件超出了初始视图)。void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: Container(
height: MediaQuery.of(context).size.height,
),
),
MyWidget(),
],
)),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SliverToBoxAdapter(
child: Container(
height: 100,
width: 100,
),
);
}
}
这是我最初编写的用于查找小部件的测试,当然失败了。
void main() {
testWidgets('main contains mywidget', (WidgetTester tester) async {
// arange
await tester.pumpWidget(MyApp());
// act
final myWidget = find.byType(MyWidget);
// assert
expect(myWidget, findsOneWidget);
});
}
在这个迭代中,我使用了
WidgetTester.scrollUntilVisible
函数,但遇到了以下错误。void main() {
testWidgets('main contains mywidget', (WidgetTester tester) async {
// arange
await tester.pumpWidget(MyApp());
// act
final myWidget = find.byType(MyWidget);
final customScrollView = find.byType(CustomScrollView);
await tester.scrollUntilVisible(myWidget, 100,
scrollable: customScrollView);
await tester.pump();
// assert
expect(myWidget, findsOneWidget);
});
}
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following _CastError was thrown running a test:
type 'CustomScrollView' is not a subtype of type 'Scrollable' in type cast
When the exception was thrown, this was the stack:
#0 WidgetController.widget (package:flutter_test/src/controller.dart:66:44)
#1 WidgetController.scrollUntilVisible.<anonymous closure> (package:flutter_test/src/controller.dart:995:15)
#2 WidgetController.scrollUntilVisible.<anonymous closure> (package:flutter_test/src/controller.dart:993:39)
#5 TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:72:41)
#6 WidgetController.scrollUntilVisible (package:flutter_test/src/controller.dart:993:27)
#7 main.<anonymous closure> (file:///C:/Users/X/test_example/test/main_test.dart:12:18)
<asynchronous suspension>
<asynchronous suspension>
(elided 3 frames from dart:async and package:stack_trace)
我很感激任何帮助或建议,以解决这个问题并成功测试我的CustomScrollView