Dart 小部件测试失败,未找到MediaQuery小部件
我的问题是关于颤振小部件测试,测试现有小部件的正确方法是什么?我找到了Dart 小部件测试失败,未找到MediaQuery小部件,dart,flutter,Dart,Flutter,我的问题是关于颤振小部件测试,测试现有小部件的正确方法是什么?我找到了MediaQuery.of,但它接受BuildContext而不是Widget 详细信息:我已经编写了简单的登录表单小部件,并尝试为其实现小部件测试。执行测试后,我得到异常: Expected: 'Sorry, only customer can login from mobile device. [Mock]' Actual: FlutterError:<No MediaQuery widget found.
MediaQuery.of
,但它接受BuildContext
而不是Widget
详细信息:我已经编写了简单的登录表单小部件,并尝试为其实现小部件测试。执行测试后,我得到异常:
Expected: 'Sorry, only customer can login from mobile device. [Mock]'
Actual: FlutterError:<No MediaQuery widget found.
Scaffold widgets require a MediaQuery widget ancestor.
The specific widget that could not find a MediaQuery ancestor was:
Scaffold-[LabeledGlobalKey<ScaffoldState>#8ffee]
The ownership chain for the affected widget is:
Scaffold-[LabeledGlobalKey<ScaffoldState>#8ffee] ← LoginForm ← [root]
Typically, the MediaQuery widget is introduced by the MaterialApp or WidgetsApp widget at
the top of your application widget tree.>
Which: FlutterError:<No MediaQuery widget found.
Scaffold widgets require a MediaQuery widget ancestor.
The specific widget that could not find a MediaQuery ancestor was:
Scaffold-[LabeledGlobalKey<ScaffoldState>#8ffee]
The ownership chain for the affected widget is:
Scaffold-[LabeledGlobalKey<ScaffoldState>#8ffee] ← LoginForm ← [root]
Typically, the MediaQuery widget is introduced by the MaterialApp or WidgetsApp widget at
the top of your application widget tree.>is not a string
When the exception was thrown, this was the stack:
#4 main.<anonymous closure> (C:\Work\app_mobile\test\login_widget_test.dart:21:5)
<asynchronous suspension>
#5 testWidgets.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter_test\src\widget_tester.dart:61:25)
#6 TestWidgetsFlutterBinding._runTestBody (package:flutter_test\src\binding.dart:471:19)
<asynchronous suspension>
#9 TestWidgetsFlutterBinding._runTest (package:flutter_test\src\binding.dart:458:14)
#10 AutomatedTestWidgetsFlutterBinding.runTest.<anonymous closure> (package:flutter_test\src\binding.dart:640:24)
#11 _FakeAsync.run.<anonymous closure> (package:quiver\testing\src\async\fake_async.dart:186:24)
#15 _FakeAsync.run (package:quiver\testing\src\async\fake_async.dart:185:11)
#16 AutomatedTestWidgetsFlutterBinding.runTest (package:flutter_test\src\binding.dart:638:16)
#17 testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test\src\widget_tester.dart:60:24)
#18 Declarer.test.<anonymous closure>.<anonymous closure> (package:test\src\backend\declarer.dart:160:19)
<asynchronous suspension>
#19 Invoker.waitForOutstandingCallbacks.<anonymous closure> (package:test\src\backend\invoker.dart:206:15)
<asynchronous suspension>
#23 Invoker.waitForOutstandingCallbacks (package:test\src\backend\invoker.dart:203:5)
#24 Declarer.test.<anonymous closure> (package:test\src\backend\declarer.dart:158:29)
<asynchronous suspension>
#25 Invoker._onRun.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test\src\backend\invoker.dart:351:23)
<asynchronous suspension>
#27 StackZoneSpecification._run (package:stack_trace\src\stack_zone_specification.dart:209:15)
#28 StackZoneSpecification._registerCallback.<anonymous closure> (package:stack_trace\src\stack_zone_specification.dart:119:48)
#33 StackZoneSpecification._run (package:stack_trace\src\stack_zone_specification.dart:209:15)
#34 StackZoneSpecification._registerCallback.<anonymous closure> (package:stack_trace\src\stack_zone_specification.dart:119:48)
#39 _Timer._runTimers (dart:isolate-patch/dart:isolate/timer_impl.dart:367)
#40 _Timer._handleMessage (dart:isolate-patch/dart:isolate/timer_impl.dart:401)
#41 _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:163)
(elided 17 frames from package dart:async and package dart:async-patch)
我找到了的
MediaQuery.of,但不明白如何将它与现有的小部件一起使用?它接受BuildContext
作为参数。您需要使用MediaQuery(…)
实例包装您的小部件,因为您使用的是Scaffold(…)
您必须将其包装在MaterialApp(…)
示例:
Widget testWidget = new MediaQuery(
data: new MediaQueryData(),
child: new MaterialApp(home: new LoginForm())
)
我也有同样的问题,我还必须用MaterialApp包装它,但我用了另一种方式,没有使用MediaQuery。就我而言,它是有效的
void main() {
Widget createWidgetForTesting({Widget child}){
return MaterialApp(
home: child,
);
}
testWidgets('Login Page smoke test', (WidgetTester tester) async {
await tester.pumpWidget(createWidgetForTesting(child: new LoginPage()));
await tester.pumpAndSettle();
});
}
**scaffold()小部件如何成为MaterialApp()的子部件的完美示例
**
导入“包装:颤振/材料.省道”;
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:我的主页(标题:“标题”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
小部件构建(构建上下文){
归还新脚手架(
appBar:appBar(
背景颜色:颜色。透明,
标题:widget.title
)
}
与下面@nknezevic提出的解决方案相比,您的解决方案有什么优势吗?MediaQuery的文档声明MaterialApp和WidgetsApp引入了MediaQuery。我是一个noob,所以我很想理解。否则,下面的答案似乎更可取,因为它是一个更干净、更简单的解决方案,或者只是简单地用一个MaterialApp。它应该像testWidget=MaterialApp(home:LoginForm());@David Shuma谢谢你的工作
Widget testWidget = new MediaQuery(
data: new MediaQueryData(),
child: new MaterialApp(home: new LoginForm())
)
void main() {
Widget createWidgetForTesting({Widget child}){
return MaterialApp(
home: child,
);
}
testWidgets('Login Page smoke test', (WidgetTester tester) async {
await tester.pumpWidget(createWidgetForTesting(child: new LoginPage()));
await tester.pumpAndSettle();
});
}
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'title'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key Key, this.title}) : super(key: Key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
title: widget.title
)
}