用后退按钮颤振测试 WillPopScope
Flutter testing WillPopScope with back button
在我的主页小部件上,当用户点击系统后退按钮时,它会通过 WillPopScope 显示一个确认对话框小部件。
我想测试这个对话框,但我不知道如何按测试文件上的后退按钮。
I cant figure it out how to press the back button on a test file.
这将帮助您看到应用栏(应用顶部)上的后退按钮。它将允许您通过 appbar
查看后退按钮
return WillPopScope(
onWillPop: _onWillPop,
child: Scaffold(
appBar: AppBar(
title: Text("Title"),
centerTitle: true,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => _onWillPop(),
),
),
body: Body(),
),
);
_onWillPop 将是您的问题的“确认对话框小部件”。
显然,您无法在测试中访问 Navigator
。所以我的解决方案是在抽取小部件的某个地方包含 BackButton
并像这样弹出页面:
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: Container(
child: BackButton(),
),
),
));
...
await tester.pageBack();
我遇到了同样的问题,但我的应用程序中没有后退按钮。我想测试 android 系统后退按钮。我就是这样做的。也许它对 运行 和我遇到同样问题的人有帮助。
testWidgets("test onWillPop",(WidgetTester tester) async {
bool willPopCalled = false;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: WillPopScope(
onWillPop: () async {
willPopCalled = true;
return false;
},
child: Container(),
),
),
),
);
final dynamic widgetsAppState = tester.state(find.byType(WidgetsApp));
await widgetsAppState.didPopRoute();
await tester.pump();
expect(willPopCalled, true);
});
灵感来自:https://github.com/flutter/flutter/blob/master/packages/flutter/test/material/will_pop_test.dart
在我的主页小部件上,当用户点击系统后退按钮时,它会通过 WillPopScope 显示一个确认对话框小部件。 我想测试这个对话框,但我不知道如何按测试文件上的后退按钮。
I cant figure it out how to press the back button on a test file.
这将帮助您看到应用栏(应用顶部)上的后退按钮。它将允许您通过 appbar
查看后退按钮return WillPopScope(
onWillPop: _onWillPop,
child: Scaffold(
appBar: AppBar(
title: Text("Title"),
centerTitle: true,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => _onWillPop(),
),
),
body: Body(),
),
);
_onWillPop 将是您的问题的“确认对话框小部件”。
显然,您无法在测试中访问 Navigator
。所以我的解决方案是在抽取小部件的某个地方包含 BackButton
并像这样弹出页面:
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: Container(
child: BackButton(),
),
),
));
...
await tester.pageBack();
我遇到了同样的问题,但我的应用程序中没有后退按钮。我想测试 android 系统后退按钮。我就是这样做的。也许它对 运行 和我遇到同样问题的人有帮助。
testWidgets("test onWillPop",(WidgetTester tester) async {
bool willPopCalled = false;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: WillPopScope(
onWillPop: () async {
willPopCalled = true;
return false;
},
child: Container(),
),
),
),
);
final dynamic widgetsAppState = tester.state(find.byType(WidgetsApp));
await widgetsAppState.didPopRoute();
await tester.pump();
expect(willPopCalled, true);
});
灵感来自:https://github.com/flutter/flutter/blob/master/packages/flutter/test/material/will_pop_test.dart