title: Nunit async test exception assertion
body: <p><strong>[Edit (May 2020)]</strong> - This issue has been reportedly addressed in newer releases of NUnit. Please see <a href="https://docs.nunit.org/articles/nunit/writing-tests/assertions/classic-assertions/Assert.ThrowsAsync.html" rel="nofollow noreferrer">Nunit.ThrowsAsync</a>. (Ref <a href="https://stackoverflow.com/a/40030988/326543">this answer</a>, thanks @James-Ross)</p>
  <hr />
  <p>I have a controller <code>UserController</code> with this action</p>
  <pre><code>// GET /blah
  public Task&lt;User&gt; Get(string domainUserName)
  {
          if (string.IsNullOrEmpty(domainUserName))
          {
              throw new ArgumentException(&quot;No username specified.&quot;);
          }
  
          return Task.Factory.StartNew(
              () =&gt;
                  {
                      var user = userRepository.GetByUserName(domainUserName);
                      if (user != null)
                      {
                          return user;
                      }
  
                      throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, string.Format(&quot;{0} - username does not exist&quot;, domainUserName)));
                  });
  }
  </code></pre>
  <p>I am trying to write a test for the case where I throw a 404 exception.</p>
  <p>Here is what I have tried, with the output -</p>
  <p>1)</p>
  <pre><code>[Test]
  public void someTest()
  {
          var mockUserRepository = new Mock&lt;IUserRepository&gt;();
          mockUserRepository.Setup(x =&gt; x.GetByUserName(It.IsAny&lt;string&gt;())).Returns(default(User));
      var userController = new UserController(mockUserRepository.Object) { Request = new HttpRequestMessage() };
  
      Assert.That(async () =&gt; await userController.Get(&quot;foo&quot;), Throws.InstanceOf&lt;HttpResponseException&gt;());
  }
  </code></pre>
  <p><strong>Result</strong>
  Test Failed</p>
  <pre><code>  Expected: instance of &lt;System.Web.Http.HttpResponseException&gt;
    But was:  no exception thrown
  </code></pre>
  <ol start="2">
  <li><p>[Test]
  public void someTest()
  {
  var mockUserRepository = new Mock();
  mockUserRepository.Setup(x =&gt; x.GetByUserName(It.IsAny())).Returns(default(User));
  var userController = new UserController(mockUserRepository.Object) { Request = new HttpRequestMessage() };</p>
  <pre><code>  var httpResponseException = Assert.Throws&lt;HttpResponseException&gt;(() =&gt; userController.Get(&quot;foo&quot;).Wait());
    Assert.That(httpResponseException.Response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
  </code></pre>
  <p>}</p>
  </li>
  </ol>
  <p><strong>Result</strong>
  Test failed</p>
  <pre><code>  Expected: &lt;System.Web.Http.HttpResponseException&gt;
    But was:  &lt;System.AggregateException&gt; (One or more errors occurred.)
  </code></pre>
  <ol start="3">
  <li></li>
  </ol>
  <pre><code>[Test]
  public void someTest()
  {
          var mockUserRepository = new Mock&lt;IUserRepository&gt;();
          mockUserRepository.Setup(x =&gt; x.GetByUserName(It.IsAny&lt;string&gt;())).Returns(default(User));
      var userController = new UserController(mockUserRepository.Object) { Request = new HttpRequestMessage() };
  
      var httpResponseException = Assert.Throws&lt;HttpResponseException&gt;(async () =&gt; await userController.Get(&quot;foo&quot;));
      Assert.That(httpResponseException.Response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
  }
  </code></pre>
  <p><strong>Result</strong>
  Test Failed</p>
  <pre><code>  Expected: &lt;System.Web.Http.HttpResponseException&gt;
    But was:  null
  </code></pre>
  <ol start="4">
  <li></li>
  </ol>
  <pre><code>[Test]
  [ExpectedException(typeof(HttpResponseException))]
  public async void ShouldThrow404WhenNotFound()
  {            var mockUserRepository = new Mock&lt;IUserRepository&gt;();
          mockUserRepository.Setup(x =&gt; x.GetByUserName(It.IsAny&lt;string&gt;())).Returns(default(User));
  
      var userController = new UserController(mockUserRepository.Object) { Request = new HttpRequestMessage() };
  
      var task = await userController.Get(&quot;foo&quot;);
  }
  </code></pre>
  <p><strong>Result</strong>
  Test passes</p>
  <p>Questions -</p>
  <ol>
  <li>Why would Assert.Throws not handle HttpResponseException, when ExpectedException does?</li>
  <li>I don't want to just test that exception is thrown. I want to assert on the Status Code of the response. What's the way to do this?</li>
  </ol>
  <p>Any comparision on these behaviour and its cause(s) would be great!</p>