Skip to content

Instantly share code, notes, and snippets.

@gamebox
Created May 1, 2019 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamebox/7a5e31d41a651dc3d67cf8bfabd0871c to your computer and use it in GitHub Desktop.
Save gamebox/7a5e31d41a651dc3d67cf8bfabd0871c to your computer and use it in GitHub Desktop.
import 'package:flutter_test/flutter_test.dart';
void main() {
Future<int> outerF;
final Duration delay = Duration(seconds: 20);
group('Async Tests - Fake Async', () {
setUpAll(() async {
outerF = Future<int>.delayed(delay, () => 42);
final Stopwatch waiting = Stopwatch()..start();
final Future<void> innerF = Future<int>.delayed(Duration(seconds: 3), () {});
await innerF;
waiting.stop();
print('Waited for ${waiting.elapsed}');
});
// Each testWidgets callback is run in a distinct FakeAsync zone
testWidgets('Delayed in test, no time added', (WidgetTester tester) async {
// This will fail due to blowing past the 2 second test case timeout.
final Future<int> f = Future<int>.delayed(delay, () => 1);
await f;
});
testWidgets('Delayed in test, time added', (WidgetTester tester) async {
final Future<int> f = Future<int>.delayed(delay, () => 1);
// So we extend the "time" allowed in the zone, and now it passes.
// Note that this still runs almost immediately.
tester.pump(delay);
await f;
});
testWidgets('Delayed from setUp, no time added', (WidgetTester tester) async {
// This will fail, and will appear to be from a timeout.
await outerF;
});
testWidgets('Delayed from setUp, time added', (WidgetTester tester) async {
// Even with the "time" added though, it will fail. It will not resolve the
// future from a different, Real Async, Zone. The error will still look like
// an exception related to a timeout.
// It will also run very slow, at least as long as the amount of "time" the
// tester binding thinks it will take.
tester.binding.addTime(delay);
await outerF;
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment