title: Async unit tests not working as expected
body: <p>I am using the latest version of NUnit (2.6.2) in Visual Studio 2012 using both resharper and the visual studio test runner. I have the follow sample tests in which I am attempting to verify that an exception is raised on an expected asynchronous method call.</p>
  
  <p>Unfortuantely, this doesn't seem to be working as expected. The first test <code>AsyncTaskCanceledSemiWorking</code> only works because i have the expectedexception attribute. The actual assert is completely ignored (as you can see by the ArgumentOutOfRange exception which is just a fake to get it to fail).</p>
  
  <p>The <code>AsyncTaskCanceledWorking</code> works fine, but doesnt test that the exception is thrown on a specified line, hence less useful.</p>
  
  <p>The third fails majestically with the below....</p>
  
  <pre><code>System.Threading.Tasks.TaskCanceledException : A task was canceled.
  Exception doesn't have a stacktrace
  </code></pre>
  
  <p>Any ideas on how I can test for the TaskCanceledException from a specific line would be very useful.</p>
  
  <p>Thanks</p>
  
  <pre><code>    [Test]
      [ExpectedException(typeof(TaskCanceledException))]
      public async Task AsyncTaskCanceledSemiWorking()
      {
          CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
          CancellationToken token = cancellationTokenSource.Token;
  
          cancellationTokenSource.Cancel();
  
          Assert.That(await LongRunningFunction(token), Throws.InstanceOf&lt;ArgumentOutOfRangeException&gt;());
  
  
      }
  
      [Test]
      [ExpectedException(typeof(TaskCanceledException))]
      public async Task AsyncTaskCanceledWorking()
      {
          CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
          CancellationToken token = cancellationTokenSource.Token;
  
          cancellationTokenSource.Cancel();
  
          int i = await LongRunningFunction(token);
      } 
  
  
      [Test]
      public async Task AsyncTaskCanceledFailed()
      {
          CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
          CancellationToken token = cancellationTokenSource.Token;
  
          cancellationTokenSource.Cancel();
  
          Assert.That(await LongRunningFunction(token), Throws.InstanceOf&lt;TaskCanceledException&gt;());
  
      }
  
      public async Task&lt;int&gt; LongRunningFunction(CancellationToken token)
      {
          token.ThrowIfCancellationRequested();
  
          await Task.Delay(1000, token);
  
          return 5;
      } 
  </code></pre>