flutter测试发现器没有找到我的状态小部件
我正在关注本教程 https://docs.flutter.dev/development/ui/interactive#the-parent-widget-manages-manages-the-widgets-state ,我在main.dart :
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
Widget statesSection = Container(
padding: const EdgeInsets.all(32),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [StatefulParentWidget()]));
return MaterialApp(
title: 'Flutter Layout',
home: Scaffold(
appBar: AppBar(title: const Text("Flutter Layout")),
body: ListView(children: [
statesSection
])));
}
它在以下测试代码中根本找不到任何内容:
testWidgets('State management tests', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
expect(find.byType(StatefulParentWidget), findsOneWidget); // Fails!
expect(find.text("Inactive"), findsOneWidget); // Fails!
expect(find.text("Active"), findsNothing); // Fails!
});
测试错误消息:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure was thrown running a test:
Expected: exactly one matching node in the widget tree
Actual: _WidgetTypeFinder:<zero widgets with type "StatefulParentWidget" (ignoring offstage
widgets)>
Which: means none were found but one was expected
任何建议和洞察力都将受到赞赏。 https://github.com/khteh/flutter
I am following this tutorial https://docs.flutter.dev/development/ui/interactive#the-parent-widget-manages-the-widgets-state and I have the following in main.dart
:
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
Widget statesSection = Container(
padding: const EdgeInsets.all(32),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [StatefulParentWidget()]));
return MaterialApp(
title: 'Flutter Layout',
home: Scaffold(
appBar: AppBar(title: const Text("Flutter Layout")),
body: ListView(children: [
statesSection
])));
}
It doesn't find anything at all in the following test code:
testWidgets('State management tests', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
expect(find.byType(StatefulParentWidget), findsOneWidget); // Fails!
expect(find.text("Inactive"), findsOneWidget); // Fails!
expect(find.text("Active"), findsNothing); // Fails!
});
Test error message:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure was thrown running a test:
Expected: exactly one matching node in the widget tree
Actual: _WidgetTypeFinder:<zero widgets with type "StatefulParentWidget" (ignoring offstage
widgets)>
Which: means none were found but one was expected
Any advice and insight is appreciated. https://github.com/khteh/flutter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大结局
将您的代码更改为:
并且测试将通过,
因此测试只能找到已经渲染的小部件,在您的情况下,
statessection
中的小部件曾经脱离舞台。以前的讨论
1与aCkaffod
如果您面对与以下相同的例外消息,
请将您的测试代码更改为此处,它应该
在此处使用完整代码:
2
如果您只想仅测试窗口小部件,则必须替换文本窗口小部件与此:
在我的第一个示例中,
scallfold
可以做到这一点,因此您可以在不说textDirection
属性的情况下进行测试。请参阅 textDirection for for进一步阅读。完整代码在这里:
Finale
Change your code to:
and the test will pass for
So the test only can find widgets rendered already, and in your case, the widgets in
statesSection
were once off the stage.Former Discussion
1 With Scaffod
If you were facing the same exception message as follows:
Change your test code to this, it should work
Full code is here:
2 Without Scaffod
If you want to test the widget solely, then you have to replace your Text widget with this:
In my first example, the
Scallfold
does the trick so you can test without tellingtextDirection
attribution. Please refer to TextDirection enum for further reading.Full code is here: