title: How to catch an error coming from a Future in flutter widget test?
body: <p>I'm facing an issue while doing widget testing on a widget that throws an exception during a <code>Future</code>.</p>
  
  <p><strong>Code to reproduce the problem</strong></p>
  
  <p>Here's a simple testwidget that reproduces the problem in a very simple way (thanks to Remi Rousselet for the simplification of the problem).</p>
  
  <pre class="lang-dart prettyprint-override"><code>testWidgets('This test should pass but fails', (tester) async {
    final future = Future&lt;void&gt;.error(42);
  
    await tester.pumpWidget(FutureBuilder(
      future: future,
      builder: (_, snapshot) {
        return Container();
      },
    ));
  });
  </code></pre>
  
  <p><strong>Expected result</strong></p>
  
  <p>I expected the test to complete without error. Instead it fails with the following error :</p>
  
  <pre><code>══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
  The number 42 was thrown running a test.
  
  When the exception was thrown, this was the stack:
  #2      main.&lt;anonymous closure&gt; (file:///C:/Projects/projet_65/mobile_app/test/ui/exception_test.dart:79:18)
  #5      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:0:0)
  #8      TestWidgetsFlutterBinding._runTest (package:flutter_test/src/binding.dart:577:14)
  #9      AutomatedTestWidgetsFlutterBinding.runTest.&lt;anonymous closure&gt; (package:flutter_test/src/binding.dart:993:24)
  #15     AutomatedTestWidgetsFlutterBinding.runTest (package:flutter_test/src/binding.dart:990:15)
  #16     testWidgets.&lt;anonymous closure&gt; (package:flutter_test/src/widget_tester.dart:106:22)
  #17     Declarer.test.&lt;anonymous closure&gt;.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (package:test_api/src/backend/declarer.dart:168:27)
  #20     Declarer.test.&lt;anonymous closure&gt;.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (package:test_api/src/backend/declarer.dart:0:0)
  #21     Invoker.waitForOutstandingCallbacks.&lt;anonymous closure&gt; (package:test_api/src/backend/invoker.dart:250:15)
  #27     Invoker._onRun.&lt;anonymous closure&gt;.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (package:test_api/src/backend/invoker.dart:399:21)
  (elided 17 frames from class _FakeAsync, package dart:async, and package dart:async-patch)
  
  The test description was:
    This test should pass but fails
  ════════════════════════════════════════════════════════════════════════════════════════════════════
  </code></pre>
  
  <p><strong>What I've tried</strong></p>
  
  <p>I've tried to <code>expect</code> the error like I would have done if it wasn't in a <code>Future</code> with:</p>
  
  <pre class="lang-dart prettyprint-override"><code>expect(tester.takeException(), equals(42));
  </code></pre>
  
  <p>But that statement fails with the following error</p>
  
  <pre class="lang-dart prettyprint-override"><code>The following TestFailure object was thrown running a test (but after the test had completed):
    Expected: 42
    Actual: null
  </code></pre>
  
  <p><strong>edit:</strong></p>
  
  <p>@shb answer is correct for the case exposed. Here's a slight modification that break it and gives back the initial error. This case is more susceptible to happen in real apps (and that's the case for me)</p>
  
  <pre class="lang-dart prettyprint-override"><code>  testWidgets('This test should pass but fails', (tester) async {
        Future future;
  
        await tester.pumpWidget(
          MaterialApp(
            home: Row(
              children: &lt;Widget&gt;[
                FlatButton(
                  child: const Text('GO'),
                  onPressed: () { future = Future.error(42);},
                ),
                FutureBuilder(
                  future: future,
                  builder: (_, snapshot) {
                    return Container();
                  },
                ),
              ],
            ),
          ));
  
        await tester.tap(find.text('GO'));
    });
  </code></pre>
  
  <p>note: I have voluntarily ommitted the <code>tester.runAsync</code> proposed by @shb to match the initial question as it does not work in that particular case</p>