title: NullReferenceException in unittesting with NUnit, NSubstitute, and async method
body: <p>I'm doing some unit testing in C# using NUnit and NSubstitute. I have a class called <code>Adapter</code>, which has a method, <code>GetTemplates()</code>, I want to unit test. <code>GetTemplates()</code> uses <code>httpclient</code>, which I have mocked out using an interface.</p>
  
  <p>The call in <code>GetTemplates</code> looks something like:</p>
  
  <pre><code>public async Task&lt;List&lt;Template&gt;&gt; GetTemplates()
  {
      //Code left out for simplificity. 
  
      var response = await _client.GetAsync($"GetTemplates");
  
      if (!response.IsSuccessStatusCode)
      { 
          throw new Exception();
      }
  
  }
  </code></pre>
  
  <p>I want <code>_client.GetAsync</code> to return a <code>HttpResponseMessage</code> with a <code>HttpStatusCode.BadRequest</code> so that I can test if the exception is being thrown.</p>
  
  <p>The test method looks like:</p>
  
  <pre><code>[Test]
  public void GetTemplate_ReturnBadRequestHttpMessage_ThrowException()
  {
       //Arrange.
       var httpMessage = new HttpResponseMessage(HttpStatusCode.BadRequest);
       _client.GetAsync("").Returns(Task.FromResult(httpMessage));
  
       //Act.
       var ex = Assert.ThrowsAsync&lt;Exception&gt;(async () =&gt; await _Adapter.GetSigningTemplates());
  
       //Assert.
       Assert.IsInstanceOf&lt;Exception&gt;(ex);
   }
  </code></pre>
  
  <p>When the method has run, it returns</p>
  
  <blockquote>
    <p>System.NullReferenceException: Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>What did I do wrong?</p>