我使用了Future.delayed以下代码,在1秒内显示了FAB:
Future.delayed(const Duration(seconds: 1), () {
setState(() {
_showFab = true;
});
});
Run Code Online (Sandbox Code Playgroud)
现在,最基本的烟雾测试已停止工作:
void main() {
testWidgets('smoke test', (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
expect(find.byType(MyHomePage), findsOneWidget);
});
}
Run Code Online (Sandbox Code Playgroud)
这是错误消息:
??? EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ?????????????????????????????????????????????????????
The following assertion was thrown running a test:
A Timer is still pending even after the widget tree was disposed.
'package:flutter_test/src/binding.dart': Failed assertion: line 933 pos 7:
'_currentFakeAsync.nonPeriodicTimerCount == 0' import 'dart:async';
Run Code Online (Sandbox Code Playgroud)
这是所有使用的代码:
import 'package:flutter/material.dart';
import 'dart:async';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _showFab = false;
@override
Widget build(BuildContext context) {
Future.delayed(const Duration(seconds: 1), () {
setState(() {
_showFab = true;
});
});
return Scaffold(
floatingActionButton: AnimatedOpacity(
opacity: _showFab ? 1.0 : 0.0,
duration: Duration(milliseconds: 1400),
child: FloatingActionButton(
onPressed: null,
child: Icon(Icons.add),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
如何更改单元测试以使测试通过?
Gün*_*uer 11
测试框架FakeAsync默认运行,它有一些限制,可能会导致你得到的错误。
您可以显式地使用异步正确运行,例如:
void main() {
testWidgets('smoke test', (WidgetTester tester) async {
await tester.runAsync(() async {
await tester.pumpWidget(MyApp());
expect(find.byType(MyHomePage), findsOneWidget);
});
});
}
Run Code Online (Sandbox Code Playgroud)
您可能仍需要其他答案中提出的建议。
小智 6
您可以使用TickerMode包装您的 Widget 。
如下所示:
void main() {
testWidgets('smoke test', (WidgetTester tester) async {
await tester.pumpWidget(TickerMode(child: MyApp(), enabled: false));
await tester.pumpAndSettle();
expect(find.byType(MyHomePage), findsOneWidget);
});
}
Run Code Online (Sandbox Code Playgroud)
尝试在泵送小部件后调用PumpAndSettle 。像这样:
void main() {
testWidgets('smoke test', (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
await tester.pumpAndSettle();
expect(find.byType(MyHomePage), findsOneWidget);
});
}
Run Code Online (Sandbox Code Playgroud)
来自文档
这本质上是等待所有动画完成。
您需要给测试人员足够的时间来处理您计划的所有异步调用,尝试将冒烟测试更改为以下内容:
void main() {
testWidgets('smoke test', (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
// Since you wait 1 second to start the animation and another 1.4 to complete it
await tester.pump(Duration(seconds: 3));
expect(find.byType(MyHomePage), findsOneWidget);
});
}
Run Code Online (Sandbox Code Playgroud)
您还需要移动Future.delayed出的build()方法,因为这是造成循环的行为,您拨打每次setState()将build()被再次调用,改变一样,您的状态:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _showFab = false;
@override
void initState() {
Future.delayed(const Duration(seconds: 1), () {
setState(() {
_showFab = true;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: AnimatedOpacity(
opacity: _showFab ? 1.0 : 0.0,
duration: Duration(milliseconds: 1400),
child: FloatingActionButton(
onPressed: null,
child: Icon(Icons.add),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
505 次 |
| 最近记录: |