title: Mock HttpResponseMessage while unit testing API with Moq and AutoFixture
body: <p>I am writing unit tests for the existing Web API 2 project. For which i am using <code>Ploeh Autofixture</code> and <code>Moq</code>.</p>
  
  <p><strong>Test Method :</strong>
     <strong>UPDATED</strong></p>
  
  <pre><code> [Test]
      public async Task Service1_TestMethod() {
  
        //some code here
       var fakeemail = FakeBuilder.Create&lt;string&gt;("test1234@test.com");
        var fakeUserInvite =
                  FakeBuilder.Build&lt;UserInvite&gt;()
                      .With(i =&gt; i.EmailAddress, fakeemail)
                      .With(i =&gt; i.Username, fakeemail)
                      .Create();
        var fakeUserToken = FakeBuilder.Create&lt;string&gt;();
        var fakeHttpResponseMessage =
                      Fixture.Build&lt;HttpResponseMessage&gt;()
                                   .With(h =&gt; h.StatusCode, HttpStatusCode.OK).Create();
    //Here i am mocking another service method. Whose response is HttpResponseMessage.
    Service2.Setup(i =&gt; i.AddUser(fakeUserInvite, fakeUserToken))
              .ReturnsAsync(fakeHttpResponseMessage);
  
    var result = await Service1.AddUser( /*   */);
  
  }
  </code></pre>
  
  <p><strong>Service1 Method :</strong></p>
  
  <pre><code>   public async Task&lt;bool&gt; AddUser(/*   */)
     {
      var response = await Service2.AddUser(userInvite, userToken); // response is null even after mocking it.
  
     // Add user code follows bassed on the above response.  
  
     }
  </code></pre>
  
  <p>If i comment the <code>Service2.AddUser</code> call then everything works. There is a lot of code in that method apart from this call. I am having problem with only this call. If this call returns the mocked <code>HttpResponseMessage</code> then everything works.</p>
  
  <p><code>Service2</code> is an external API. I am just wondering how to <code>mock</code> <code>HttpResponseMessage</code>. Any help is appreciated.</p>