unit-testing - Flutter/Dart 在单元测试中等待几秒钟
问题描述
我正在编写一个计时器应用程序。在单元测试中,如何等待几秒钟来测试我的计时器是否正常工作?
// I want something like this.
test("Testing timer", () {
int startTime = timer.seconds;
timer.start();
// do something to wait for 2 seconds
expect(timer.seconds, startTime - 2);
});
解决方案
您可以使用await
Future.delayed(...)`:
test("Testing timer", () async {
int startTime = timer.seconds;
timer.start();
// do something to wait for 2 seconds
await Future.delayed(const Duration(seconds: 2), (){});
expect(timer.seconds, startTime - 2);
});
另一种方法是使用https://pub.dartlang.org/packages/clock的 fake_async,以便能够自由地操纵测试中使用的时间。
推荐阅读
- django - OneToOne 的妈妈食谱
- java - 为什么 Java.NIO 使用静态方法?
- extjs - extjs Ext.app.Application 用于自定义包
- scala - Akka-stream 仅组 Either 的右元素
- python - 如何使用 Chromedriver 下载到特定文件夹?
- python - 如何识别开放式简历中的头部运动?
- java - Mockito 在 when 方法中捕获间谍对象的值
- arrays - 在 RXJS 中传递给 switchMap() 时对象数组的奇怪行为
- csl - 引用引用中的单个引用可以超过一个 url/访问日期对吗?
- ios - 关闭应用程序时如何修复maccatalyst中的自定义方案打开?