title: How to write unit test case IHttpClientFactory
body: <p>I have a method in service layer to  connect to a service and i am using <code>IHttpClientFactory</code>. My method is working fine. Now i am trying to write unit test cases for the same.</p>
  <pre><code>public async Task&lt;MyObject&gt; MyMethodAsync(string arg1, string arg2)
  {
      var client = _httpClientFactory.CreateClient(&quot;XYZ&quot;);
  
      var Authkey = &quot;abc&quot;; 
      var AuthToken = &quot;def&quot;;
         
      var headers = new Dictionary&lt;string, string&gt;
          {
              { Authkey,AuthToken }
          };
  
      client.AddTokenToHeader(headers); //This method set  the DefaultRequestheader from the dictionary object
  
      var reqData = new
      {
          prop1 = &quot;X&quot;, 
          prop2 = &quot;Y&quot;
      };//req object
  
      var content = new StringContent(JsonConvert.SerializeObject(reqData), Encoding.UTF8, &quot;application/json&quot;);
  
      //This is httpClient Post call for posting data 
      HttpResponseMessage response = await client.PostAsync(&quot;postData&quot;, content); 
      if (!response.IsSuccessStatusCode || response.Content == null)
      {
          return null;
      }
  
      MyObject myObject = JsonConvert.DeserializeObject&lt;MyObject&gt;(response.Content.ReadAsStringAsync().Result);//read the result to an object
      return myObject;
  }
  </code></pre>
  <p>For the above method i am writing test cases. Here i am trying to set the Post methods out put a status code OK and expecting the
  <code>MyMethodAsync</code> method to be true as the <code>PostAsync</code> is true. Here i am getting an exception</p>
  <blockquote>
  <p>System.InvalidOperationException : An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set.</p>
  </blockquote>
  <pre><code>[Test]
  public async Task MyMethodAsync_Gets_True()
  {
      var response = new HttpResponseMessage(HttpStatusCode.OK)
      {
          Content = new StringContent(&quot;It worked!&quot;)
      };
  
      //Mock the httpclientfactory
      var _httpMessageHandler = new Mock&lt;HttpMessageHandler&gt;();
      var mockFactory = new Mock&lt;IHttpClientFactory&gt;(); 
  
      //Specify here the http method as post
      _httpMessageHandler.Protected()
        .Setup&lt;Task&lt;HttpResponseMessage&gt;&gt;(&quot;SendAsync&quot;,
         ItExpr.Is&lt;HttpRequestMessage&gt;(req =&gt; req.Method == HttpMethod.Post),
         ItExpr.IsAny&lt;CancellationToken&gt;())
         .ReturnsAsync(new HttpResponseMessage
         {
             StatusCode = HttpStatusCode.OK
         });
  
  
      var httpClient = new HttpClient(_httpMessageHandler.Object);
      mockFactory.Setup(_ =&gt; _.CreateClient(It.IsAny&lt;string&gt;())).Returns(httpClient);
  
      var arg1 = &quot;X&quot;;
      var arg2 = &quot;D101&quot;;
  
      var service = new MyService(_mockAppSettings.Object, mockFactory.Object);
      var result = await service.MyMethodAsync(arg1, arg2);
      // Assert
      Assert.IsNotNull(result);
  }
  </code></pre>
  <p>Can someone show what mistake i am doing here?</p>