title: Silverlight async unit testing
body: <p>I'm having a weird issue with Silverlight Unit Test Framework. The very first method executed fails, every time. I have a second test with the exact same code, and it passes. The strange thing about the first time it's called is that it actually waits for the timeout <em>and then</em> executes the repository call (underneath it's an HTTP PUT if you care). Here's the code - the first one fails every time, second one passes every time:</p>
  
  <pre><code>    [TestMethod]
      public void AuthShouldSucceed()
      {
          var autoResetEvent = new AutoResetEvent(false);
  
          _authRepository.Authenticate(_username, _password, response =&gt;
          {
              Assert.IsTrue(response);
              autoResetEvent.Set();
          });
          if (!autoResetEvent.WaitOne(Constants.Timeout))
          {
              Assert.Fail("Test timed out.");
          } 
      }
  
      [TestMethod]
      public void AuthShouldSucceed2()
      {
          var autoResetEvent = new AutoResetEvent(false);
  
          _authRepository.Authenticate(_username, _password, response =&gt;
          {
              Assert.IsTrue(response);
              autoResetEvent.Set();
          });
          if (!autoResetEvent.WaitOne(Constants.Timeout))
          {
              Assert.Fail("Test timed out.");
          } 
      }
  </code></pre>
  
  <p><strong>Edit:</strong>
  My final solution is a modification of Vladmir's solution:</p>
  
  <pre><code>    [TestMethod]
      [Asynchronous]
      public void AuthShouldSucceed()
      {
          var complete = false;
          var result = false;
  
          _authRepository.Authenticate(_username, _password, response =&gt;
          {
              complete = true;
              result = response;
          });
  
          EnqueueConditional(() =&gt; complete);
          EnqueueCallback(() =&gt; Assert.IsTrue(result));
          EnqueueTestComplete();
      }
  </code></pre>