【问题标题】:Widget testing TextFormField obscureText value小部件测试 TextFormField obscureText 值
【发布时间】:2021-11-13 01:42:33
【问题描述】:

我似乎无法找到一种方法来测试 TextFormField 是否将 obscureText 设置为 true 或 false。

我知道 TextFormField 是 TextField 的包装器,而 obscureText 是 TextField 的一个属性,但我想不出在我的测试中访问 obscureText 的方法。

所以,当我尝试访问 obscureText 时,我收到消息:getter 'obscureText' 没有为类型 'TextFormField' 定义

有人知道如何测试 TextFormField 的 obscureText 属性吗?

  testWidgets(
    'show and hide password fields',
    (
      WidgetTester tester,
    ) async {
      await tester.pumpWidget(
        materialWrapper(
          child: emailSignUpScreen,
        ),
      );
      final Finder passwordTextFormField = find.byKey(
        const Key(
          'passwordTextFormField',
        ),
      );
      final TextFormField input =
          tester.widget<TextFormField>(passwordTextFormField);
      expect(input.obscureText, true);
    },
  );

【问题讨论】:

    标签: flutter-test flutter-textformfield


    【解决方案1】:

    TextFormField 使用EditableText 来掩盖它,你可以这样做:

    final passwordTextFormField = find.descendant(
      of: find.byKey(const Key('passwordTextFormField')),
      matching: find.byType(EditableText),
    );
    final input = tester.widget<EditableText>(passwordTextFormField);
    expect(input.obscureText, isTrue);
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 2021-05-09
      • 2019-02-25
      • 1970-01-01
      • 2019-12-27
      • 2019-06-13
      • 2021-01-21
      相关资源
      最近更新 更多