【问题标题】:Flutter unit test case exits when testing a method with Future.delayed使用 Future.delayed 测试方法时 Flutter 单元测试用例退出
【发布时间】:2021-06-13 14:44:48
【问题描述】:

我正在使用flutter_bloc。

  @override
  Stream<MyState> mapEventToState(
      MyEvent event) async* {
     if (event is SomeEvent) {
      yield await _fetchdata();
    }
  }

  Future< MyState> _fetchdata() async {
    await Future.delayed(Duration(seconds: 5));
    return MyState();
  }

在测试 bloc 并添加 SomeEvent 时,测试用例会在调用 Future.delayed 的地方中断。

【问题讨论】:

  • 这是您的实际代码吗? _fetchData 需要标记为 async 才能使用 await 关键字。你的测试用例是如何破坏的?有错误信息吗?
  • @jamesdlin 不幸的是,我无法分享实际代码。但这与实际情况接近。异步是原始问题中的遗漏。我已经更新了。
  • 如果您无法分享您的实际代码,请编写一个您可以分享的可重现案例。此外,您说“测试用例中断”而没有提供有关失败的任何详细信息。

标签: flutter unit-testing dart


【解决方案1】:

遇到同样的问题,找到解决方案here

这里有一段引述:

我的 initstate 中有一个 Future.delayed(5 secs ),所以我的测试失败了,一些 Pending Timers 错误通过为我添加这个来快速转发时间。

await tester.pumpAndSettle(const Duration(seconds: 5));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-18
    • 2023-03-27
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2022-11-29
    相关资源
    最近更新 更多