【问题标题】:Flutter, How to make a unit test on a Widget that contain AppLocalization (multilingual)?Flutter,如何对包含 AppLocalization(多语言)的 Widget 进行单元测试?
【发布时间】:2021-06-30 08:29:50
【问题描述】:

我正在尝试对我的 HistoryPage 小部件进行简单测试,这里是:

class HistoryPage extends StatefulWidget {
  @override
  _HistoryPageState createState() => _HistoryPageState();
}

class _HistoryPageState extends State<HistoryPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(AppLocalizations.of(context)!.historyPageTitle),
        backgroundColor: Colors.green,
      ),
      body: Column(
        children: [
        ],
      ),
    );
  }
}

这是该小部件的单元测试:

  group('UI TESTS', () {
    Widget makeTesteableWidget({required Widget child}) {
      return MaterialApp(
        home: child,
      );
    }

    testWidgets('History page', (WidgetTester tester) async {
      // Create the widget by telling the tester to build it.
      final widget = makeTesteableWidget(
        child: HistoryPage(),
      );

      await tester.pumpWidget(widget);

      expect(true, true);
    });
  });

这是我运行测试时的异常:

══╡ 小部件库发现异常╞═══════════════════════╕═══╕═␕═════════════════ ═════════════════════ 以下 _CastError 被抛出构建 HistoryPage(dirty, dependencies: [_LocalizationsScope-[GlobalKey#ca13b]],状态:_HistoryPageState#b1cad): 用于空值的空检查运算符

如果我像这样替换 AppBar 标题:

appBar: AppBar(
            title: Text('Simple title'),
            backgroundColor: Colors.green,
          ),

测试正在运行...但我不知道如何管理此行以不引发此异常...

title: Text(AppLocalizations.of(context)!.historyPageTitle),

感谢您的帮助!

【问题讨论】:

    标签: flutter unit-testing testing localization multilingual


    【解决方案1】:

    发现问题!在测试中,在 MaterialApp 中,我刚刚调用了本地化(就像在主程序中一样),如下所示:

    group('UI TESTS', () {
        Widget makeTesteableWidget({required Widget child}) {
          return MaterialApp(
            localizationsDelegates: [
              AppLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            supportedLocales: [
              const Locale('en', ''),
              // English, no country code
              const Locale('fr', ''),
              // French, no country code
              const Locale('de', ''),
              // German, no country code
              // const Locale('gsw', ''), // Swiss German Alemannic Alsatian, no country code
              const Locale('it', ''),
              // Italian, no country code
            ],
            home: child,
          );
        }
    
        testWidgets('History page', (WidgetTester tester) async {
          // Create the widget by telling the tester to build it.
          final widget = makeTesteableWidget(
            child: LoginPage(),
          );
    
          await tester.pumpWidget(widget);
    
          expect(true, true);
        });
      });
    

    【讨论】:

    • 酷。我也有这个问题,我们根据条件将本地化放在小部件中。让我测试一下。谢谢
    猜你喜欢
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 2020-11-13
    • 2021-10-20
    • 1970-01-01
    相关资源
    最近更新 更多