【发布时间】: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