title: UnitTest example for asynchronous code
body: <p>After reading the <a href="http://www.dartlang.org/articles/dart-unit-tests/#asynchronous-tests" rel="nofollow">Unit Testing with Dart</a> somehow I'm still can not understand how to use it with <code>Future</code>s.</p>
  
  <p><strong>For example:</strong></p>
  
  
  
  <pre class="lang-dart prettyprint-override"><code>void main()
  {
      group('database group',(){
      setUp( () {
                  // Setup
             });
  
      tearDown((){
                  // TearDown
             });
  
      test('open connection to local database', (){
          DatabaseBase database = null;
  
          expect(database = new MongoDatabase("127.0.0.8", "simplechat-db"), isNotNull);
  
          database.AddMessage(null).then(
              (e) {
                    expectAsync1(e) 
                    {
                       // All ok
                    }
                  },
              onError: (err)
                       {
                          expectAsync1(bb)
                          {
                            fail('error !');
                          }
                       }
           );
  
  });
  
  // Add more tests here
  </code></pre>
  
  <p>});
  }</p>
  
  <p>So in the test I create an instance of base abstract class <code>DatabaseBase</code> with some parameters to actual <a href="https://github.com/vadimtsushko/mongo_dart" rel="nofollow">MongoDb</a> class and immediately check if it created. Then I just run some very simple function: <code>AddMessage</code>. This function defined as:</p>
  
  <pre class="lang-dart prettyprint-override"><code>Future AddMessage(String message);
  </code></pre>
  
  <p>and return <code>completer.future</code>. </p>
  
  <p>If passed <code>message</code> is null then the function will fail completer as: <code>.completeError('Message can not be null');</code></p>
  
  <p>In actual test I want to test if <code>Future</code> completed successfully or with error. So above this is my try to understand how to test <code>Future</code> returns - the problems is this this test <strong>does not fail</strong>  :(</p>
  
  <p>Could you write in answer a little code example how to test functions that return <code>Future</code> ? And in test I mean - sometimes I want to test return (on success) value and fail test if success value is incorrect and another test should fail then function will fail <code>Future</code> and enter to <code>onError:</code> block.</p>