我在颤振项目中使用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 个print
应该分别打印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!
我怎样才能解决这个问题?