Flutter测试无法找到AlertDialog小部件

4
我目前在编写一个测试,用于输入不匹配的密码信息,以检查我们的AlertDialog小部件是否显示文本:“密码不匹配!”
以下是我的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);
  });
1个回答

2

您需要为AlertDialog添加actions属性。

AlertDialog(
 title: Text("My title"),
 content: Text("This is my message."),
 actions: [
   okButton,
 ],
);

//Widget
  Widget okButton = TextButton(
    child: Text("OK"),
    onPressed: () { },
  );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接