【发布时间】:2021-09-30 11:46:17
【问题描述】:
我在颤振项目中使用easy_localization。 我需要编写一些小部件测试。
但看起来当带有翻译的.json 文件太大时,MaterialApp 永远不会构建它的child,因此我无法测试我的小部件。
这是我的小型可重现项目的架构:
my_project
|- assets
| |- lang
| | |- ru.json
| | |- sv.json
|- test
| |- test_test.dart
这是我的test_test.dart 文件:
import 'package:easy_localization/easy_localization.dart';
import 'package:easy_logger/easy_logger.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
testWidgets('', (tester) async {
await tester.runAsync(() async {
SharedPreferences.setMockInitialValues({});
EasyLocalization.logger.enableLevels = <LevelMessages>[
LevelMessages.error,
LevelMessages.warning,
];
await EasyLocalization.ensureInitialized();
await tester.pumpWidget(
EasyLocalization(
supportedLocales: const [Locale('sv')], // <- Change it to 'ru' and it doesn't work
path: 'assets/lang',
child: Builder(
builder: (context) {
print('builder1');
return MaterialApp(
locale: EasyLocalization.of(context).locale,
supportedLocales: EasyLocalization.of(context).supportedLocales,
localizationsDelegates: EasyLocalization.of(context).delegates,
home: Builder(
builder: (context) {
print('builder2');
return const SizedBox.shrink();
},
),
);
},
),
),
);
await tester.pumpAndSettle();
});
});
}
sv.json(一个小文件):
{
"0": "0"
}
ru.json(一个大文件):
{
"0": "0 - xx ... xx", // <- 1000 "x"
"1": "1 - xx ... xx", // <- 1000 "x"
// ...
"3998": "3998 - xx ... xx", // <- 1000 "x"
"3999": "3999 - xx ... xx" // <- 1000 "x"
}
在我的测试中,我有 2 个 prints 应该分别打印 builder1 和 builder2。
当我在测试中使用Locale('sv') 时效果很好:
00:02 +0:
builder1
builder2
00:02 +1: All tests passed!
但是当我使用Locale('ru') 时,MaterialApp 不会生成它的孩子,我也没有得到打印builder2:
00:03 +0:
builder1
00:03 +1: All tests passed!
我该如何解决这个问题?
【问题讨论】:
-
您能否发布您的存储库以便我们克隆它并重现该问题?你使用的是哪个版本的 Flutter?
-
看起来这是库中的错误。他们正在调查它github.com/aissat/easy_localization/issues/…
标签: flutter localization flutter-test flutter-localizations