Flutter 测试找不到 AlertDialog 小部件
Flutter Test Cannot Find AlertDialog Widget
我目前正在编写一个测试,以输入密码不匹配的用户信息,以查看我们的 AlertDialog 小部件是否显示文本:“密码不匹配!”
这是我的警报对话框所在的位置:
if (passwordController.text != reenterController.text) {
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
key: Key('signUpADKey'),
content: Text('Passwords do not match!'),
);
},
);
}
我会注意到这个 if 语句位于 ElevatedButton onPressed() 部分的内部。
此外,这是我的测试:
testWidgets('Miss-matching Passwords Test', (WidgetTester tester) async {
SignUpPage page = SignUpPage();
await tester.pumpWidget(makeTestableApp(page));
await tester.enterText(
find.byKey(const Key('firstNameTextFormField')), 'Jimmy');
await tester.enterText(
find.byKey(const Key('lastNameTextFormField')), 'Lewis');
await tester.enterText(find.byKey(const Key('shortBioTextFormField')),
'I go to the University of ABC.');
await tester.enterText(
find.byKey(const Key('emailTextFormField')), 'jimmylewis@abc.edu');
await tester.enterText(
find.byKey(const Key('passwordTextFormField')), 'Test1234!');
await tester.enterText(
find.byKey(const Key('reEnterTextFormField')), 'Test4321!');
await tester.pump();
await tester.tap(find.text('Sign Up'));
await tester.pump();
expectLater(find.byKey(const Key('signUpADKey')), findsOneWidget);
});
您必须为 AlertDialog 添加操作。
AlertDialog(
title: Text("My title"),
content: Text("This is my message."),
actions: [
okButton,
],
);
//Widget
Widget okButton = TextButton(
child: Text("OK"),
onPressed: () { },
);
我目前正在编写一个测试,以输入密码不匹配的用户信息,以查看我们的 AlertDialog 小部件是否显示文本:“密码不匹配!”
这是我的警报对话框所在的位置:
if (passwordController.text != reenterController.text) {
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
key: Key('signUpADKey'),
content: Text('Passwords do not match!'),
);
},
);
}
我会注意到这个 if 语句位于 ElevatedButton onPressed() 部分的内部。
此外,这是我的测试:
testWidgets('Miss-matching Passwords Test', (WidgetTester tester) async {
SignUpPage page = SignUpPage();
await tester.pumpWidget(makeTestableApp(page));
await tester.enterText(
find.byKey(const Key('firstNameTextFormField')), 'Jimmy');
await tester.enterText(
find.byKey(const Key('lastNameTextFormField')), 'Lewis');
await tester.enterText(find.byKey(const Key('shortBioTextFormField')),
'I go to the University of ABC.');
await tester.enterText(
find.byKey(const Key('emailTextFormField')), 'jimmylewis@abc.edu');
await tester.enterText(
find.byKey(const Key('passwordTextFormField')), 'Test1234!');
await tester.enterText(
find.byKey(const Key('reEnterTextFormField')), 'Test4321!');
await tester.pump();
await tester.tap(find.text('Sign Up'));
await tester.pump();
expectLater(find.byKey(const Key('signUpADKey')), findsOneWidget);
});
您必须为 AlertDialog 添加操作。
AlertDialog(
title: Text("My title"),
content: Text("This is my message."),
actions: [
okButton,
],
);
//Widget
Widget okButton = TextButton(
child: Text("OK"),
onPressed: () { },
);