title: How do I write a unit test for a Flutter method that completes later with a future?
body: <p>I'm writing a unit test for a Flutter method that calls an async method and then returns, leaving the async to complete as and when. My test fails "after it had already completed".</p>
  
  <p>Here's my test:</p>
  
  <pre><code>    test('mark as viewed', () {
        final a = Asset();
        expect(a.viewed, false);
        a.markAsViewed();
        expect(a.viewed, true);
      });
  </code></pre>
  
  <p>and here's the method it's testing:</p>
  
  <pre><code>  void markAsViewed() {
      viewed = true;
      Repository.get().saveToStorage();
    }
  </code></pre>
  
  <p>The <code>saveToStorage()</code> method is an async that I just leave to execute in the background.</p>
  
  <p>How do I make this work? The test failure tells me <code>Make sure to use [expectAsync] or the [completes] matcher when testing async code.</code> but I can't see how to do that. Can anyone explain or else point me to the right documentation please? I can't find anything about how to handle these asyncs when it's not a <code>Future</code> that's being returned, but just being left to complete separately.</p>
  
  <p>To be clear - this unit test isn't about testing whether it's saved to storage, just a basic test on setting <code>viewed</code> to be <code>true</code>.</p>
  
  <p><em>Edited</em></p>
  
  <p>The error is as follows:</p>
  
  <pre><code>package:flutter/src/services/platform_channel.dart 319:7  MethodChannel.invokeMethod
  ===== asynchronous gap ===========================
  dart:async                                                _asyncErrorWrapperHelper
  package:exec_pointers/asset_details.dart                  Repository.saveToStorage
  package:exec_pointers/asset_details.dart 64:22            Asset.markAsViewed
  test/asset_details_test.dart 57:9                         main.&lt;fn&gt;.&lt;fn&gt;
  This test failed after it had already completed. Make sure to use [expectAsync]
  or the [completes] matcher when testing async code.
  </code></pre>