【问题标题】:Setting locale in flutter integration test using integration_test package使用 integration_test 包在颤振集成测试中设置语言环境
【发布时间】:2020-08-29 19:03:43
【问题描述】:

我正在尝试使用integration_test 包进行测试,该包在运行测试之前设置了特定的语言环境。我尝试了以下方法(接近 WidgetTests 中的方法):

    await tester.binding.setLocale('en', 'US');
    app.main();
    await tester.idle();
    await tester.pumpAndSettle();

    // The app is still using the default locale of the phone...

这需要在驱动中设置吗?这是我在驱动程序中的当前设置:

  // Some adb commands for granting permissions...
  print('Starting test.');
  final FlutterDriver driver = await FlutterDriver.connect();
  final String data = await driver.requestData(
    null,
    timeout: const Duration(minutes: 1),
  );
  await driver.close();
  // Some more adb commands to revoke permissions.

这似乎不起作用。

我找到了this issue here,但它没有使用integration_test 包,因此完全不同。

【问题讨论】:

    标签: flutter integration-testing


    【解决方案1】:

    我能够通过将目标小部件包装在 Localizations 小部件中来本地化我的集成测试。比如:

    class WidgetTestWrapper extends StatelessWidget {
      const WidgetTestWrapper({
        Key? key,
        required this.locale,
        required this.child,
      }) : super(key: key);
    
      final Widget child;
      final Locale locale;
    
      static const localizationsDelegates = <LocalizationsDelegate>[
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ];
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Material(
            child: Localizations(
              locale: locale,
              delegates: localizationsDelegates,
              child: child,
            ),
          ),
        );
      }
    }
    

    然后我在调用runApp 时传入所需的Locale 进行测试。

    例如,对于美国西班牙语 (es-US):

    runApp(WidgetTestWrapper(locale: const Locale('es', 'US'), child: widgetUnderTest));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 2022-08-11
      • 1970-01-01
      • 2011-01-24
      相关资源
      最近更新 更多