android - 在 flutter 测试中比较两个 PinCode 文本字段

标签 android ios flutter flutter-test

目前我正在工作的不是我使用此插件的项目:https://pub.dev/packages/pin_code_fields

所以问题是我很困惑我们应该在什么级别上测试小部件(小部件测试)。

  1. 使用类型键检查小部件是否存在于树中,稍后根据类型进行期望。
  2. 我在使用泵的测试中检查了引脚输入是否为 1,并输入文本然后进行比较。
  3. 我有点困惑如何检查两个 PinCodeTextField 并检查它们是否具有相似的代码。

任何人都可以告诉我们在 flutter 测试中要考虑什么(在什么限制下停止)。 我正在添加一些示例代码,其中我已经介绍了前两点。

void main() {
  Widget _wrapWithMaterialApp(Widget widget) => MaterialApp(home: Scaffold(
    body: widget,
  ));
  testWidgets('PinCode text field testing',  (WidgetTester tester) async {
    
    await tester.pumpWidget(_wrapWithMaterialApp(AppPinWidget(
        controller: TextEditingController())));

    var widget = find.byType(AppPinWidget);
    expect(widget, findsOneWidget);


    final textField = find.descendant(
      of: find.byType(PinCodeTextField),
      matching: find.byType(AnimatedContainer),
    );
    await tester.tap(textField.first);
    await tester.pump();
    await tester.enterText(find.byType(TextFormField).first, '3');
    await tester.pump();
    expect(find.text('3'), findsWidgets);
    await tester.pump(const Duration(milliseconds: 300));

    await tester.enterText(find.byType(TextFormField).first, '4');
    await tester.pump();
    expect(find.text('4'), findsWidgets);
    await tester.pump(const Duration(milliseconds: 300));
    await tester.enterText(find.byType(TextFormField).first, '5');
    await tester.pump();
    expect(find.text('5'), findsWidgets);
    await tester.pump(const Duration(milliseconds: 300));
    await tester.enterText(find.byType(TextFormField).first, '6');
    await tester.pump();
    expect(find.text('6'), findsWidgets);
    await tester.pump(const Duration(milliseconds: 300));
    expect(find.byType(PinCodeTextField), findsOneWidget);

  });
}

最佳答案

我不太明白问题是什么,但我将在这里留下一段用于比较文本内容相等性的代码。有几种方法

expect(someTextWidget.data, equals(someOtherTextWidget.data));

expect(find.byWidgetPredicate((widget) =>
              widget is PinCodeTextField &&
              widget.data == 'expectedValue')),
          findsNWidgets(2)); // here 2 is how many widgets you should have with the same text

expect(someTextWidget.data,
          equals(someOtherTextWidget.data));

expect(pinTextFirstFieldKey.text, equals(pinTextSecondFieldKey.text)
//it is if you try to compare FormTextFields with assigned global keys of type GlobalKey<FormFieldState<String>>()

如果有不清楚的地方,请在评论中指出 - 我会添加编辑。

关于android - 在 flutter 测试中比较两个 PinCode 文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72479339/

相关文章:

java - Google map V2 - Android - invalidate() 等效项

android - 如何使用 kivy StringProperty?

android - 保持应用程序处于运行状态以接收来自 Urban Airship 的推送

ios - 获取 iOS 照片库中所有照片的数组,其中包含面孔

flutter - 无法从项目中更改或删除日志

android - AWS-放大-Appsync : What is the correct workflow when using CLI to make changes to the cloud resources from an Android app?

objective-c - iPhone - 找不到我的应用程序

ios - 手机间隙 : Scroll top on statusbar tap

flutter : TextField with FocusNode does not get called when clicked outside of TextField

ios - 由于在应用程序内购买,导致App Store Flutter应用程序被拒绝