我正在编写一个计时器应用程序。在单元测试中,如何等待几秒钟来测试我的计时器是否正常工作?
// 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能够自由地操纵测试中使用的时间。
https://stackoverflow.com/questions/54088397/
相关文章:
android - 如何在 Flutter 中的图像上显示文本?
dart - 是否可以使用 Flutter 提供的 Dart SDK 作为 AngularDart
dart - flutter : Get Local position of Gesture Det
dart - 没有 AppBar 的 Flutter 应用设计
flutter - 如何停止 flutter 应用程序中的firebase错误
flutter - Flutter中的Material和MaterialApp有什么区别?
dart - 如何在 Flutter 中获取 Text 小部件的大小