简体   繁体   English

Flutter 集成测试 - 坏 state,无元素

[英]Flutter integration test - bad state, no element

In my integration tests I have to keep doing: await tester.pumpAndSettle(const Duration(seconds: [some duration]));在我的集成测试中,我必须继续做: await tester.pumpAndSettle(const Duration(seconds: [some duration])); to wait for the widgets to load to the screen after animation delays, otherwise I get the error: "bad state: no element".在 animation 延迟后等待小部件加载到屏幕上,否则我会收到错误消息:“坏 state:没有元素”。

Is this the best way to handle this issue?这是处理这个问题的最好方法吗? I find it is very flaky now that I am trying to add integration tests to my continuous integration platform.我发现现在我正在尝试将集成测试添加到我的持续集成平台中,这非常不稳定。 I have had to add more time to the duration to wait which feels quite yuck (race condition) and will also increase the costs of continuous integration due to build times taking longer.我不得不增加更多的等待时间,这感觉很糟糕(竞争条件),并且由于构建时间更长,也会增加持续集成的成本。 Here is an example of the code:以下是代码示例:

Future<void> registerUserPasswordButtonShouldBeDisabled() async {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  testWidgets('''registering a new user with non-unique email address disables 
  register password button''', (WidgetTester tester) async {
    await app.main();
    await tester.pumpAndSettle(const Duration(seconds: 5));

    final registerButton = find.byKey(const Key('register'));

Is there a better way to handle the "bad state: no element" error due to delayed animations causing widgets to not appear on the screen until some delay?有没有更好的方法来处理“糟糕的 state:无元素”错误,这是由于动画延迟导致小部件直到一些延迟才出现在屏幕上?

Have you tried without the duration parameter?您是否尝试过不使用持续时间参数?

await tester.pumpAndSettle();

If you have an animating loading spinner and some transitions before your button is shown, then pumpAndSettle will wait till all those animations have stopped and not a millisecond longer than the default 100ms which isn't a big waste of time.如果您在显示按钮之前有一个动画加载微调器和一些过渡,那么pumpAndSettle将等待所有这些动画停止并且不会比默认的 100 毫秒长一毫秒,这不会浪费时间。

Reference: the docs参考: 文档

I had the same problem and wrote a small replacement for waitFor :我遇到了同样的问题,并为waitFor写了一个小替代品:

Future<void> waitFor(Finder finder, [timeoutInSeconds = 10]) {
  Completer c = Completer();
  Timer poll;
  Timer timeout;

  poll = Timer.periodic(Duration(milliseconds: 500), (_) {
    if (finder.evaluate().isNotEmpty) {
      poll.cancel();
      timeout.cancel();
      c.complete();
    }
  });

  timeout = Timer(Duration(seconds: timeoutInSeconds), () {
    poll.cancel();
    c.completeError('WaitFor timed out for ${finder.description}');
  });

  return c.future;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 坏 state:没有元素 flutter - Bad state: No element flutter Flutter 坏 State 无元件 - Flutter Bad State No Element Flutter StateError(错误的 state:无元素) - Flutter StateError (Bad state: No element) flutter 在扩展 EditableText “坏 state:无元素”的小部件上测试 enterText:构建可编辑文本以供提及 - flutter test enterText on widget extending EditableText “Bad state: No element” : building editable text for mention flutter:发生异常:错误 state:XML 中没有元素 - flutter: Exception Happened: Bad state: No element in XML “错误的 state:无元素”错误 - Flutter Future Builder - 'Bad state: No element' Error - Flutter Future Builder 使用 FutureBuilder 和 Provider 在 Flutter 中没有元素的错误状态 - Bad state no element in Flutter Using FutureBuilder and Provider Flutter集成测试,如何确保用户退出? (冲洗状态) - Flutter integration test, how to ensure user is signed out? (flushed state) Flutter | 未处理的异常:错误 state:没有使用“firstWhere”和“orElse”的元素 - Flutter | Unhandled Exception: Bad state: No element using 'firstWhere' and 'orElse' 等待调试连接时出错:状态不佳:没有元素 Flutter VS Code - Error waiting for a debug connection: Bad state: No element Flutter VS Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM