title: How to mock the new HttpClientFactory in .NET Core 2.1 using Moq
body: <p>.NET Core 2.1 comes with this new factory called <code>HttpClientFactory</code>, but I can't figure out how to mock it to unit test some methods that include REST service calls.</p>
  
  <p>The factory is being injected using .NET Core IoC container, and what the method does is create a new client from the factory:</p>
  
  <pre><code>var client = _httpClientFactory.CreateClient();
  </code></pre>
  
  <p>And then using the client to get data from a REST service:</p>
  
  <pre><code>var result = await client.GetStringAsync(url);
  </code></pre>