Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Tests: Waiting for a certain duration

Tags:

I'm trying to wait for some time after I tested tapping my button to then check the result with expect. I'm using Future.delayed for that. But that doesn't work for me. I'm getting a time out error.

  TimeoutException after 0:00:05.000000: Test timed out after 5 seconds.

This is the code I use:

... // other tests
await tester.tap(find.widgetWithText(GestureDetector, "ref size"));
await new Future.delayed(new Duration(milliseconds: 50));
expect(testContainerState.childWidth, 50.0);

Does any one have an idea why this (imo) strange behavior occurs?

like image 776
Bram Vanbilsen Avatar asked Mar 30 '18 20:03

Bram Vanbilsen


People also ask

How do you wait for a few seconds in flutter?

start(); // do something to wait for 2 seconds await Future. delayed(const Duration(seconds: 2), (){}); expect(timer. seconds, startTime - 2); });

How do you wait in flutter test?

So, to start out with a simpler answer the correct way to wait for a period of time in a flutter test is using tester. pump . await tester. pump(new Duration(milliseconds: 50));


1 Answers

So, to start out with a simpler answer the correct way to wait for a period of time in a flutter test is using tester.pump.

await tester.pump(new Duration(milliseconds: 50));

The longer answer to why this happens has to do with the flutter testing environment. To make sure tests are reliable, even in the face of time-varying animations the environment mocks as much of the async behavior as possible, using utilities such as FakeAsync from package:quiver.

like image 119
Jonah Williams Avatar answered Sep 21 '22 14:09

Jonah Williams