When your test project references Microsoft.Extensions.Http, this generator creates a WithHttpClientFactory() extension method for AutoMocker that provides a testable IHttpClientFactory implementation.
- Automatically generates when
Microsoft.Extensions.Httpis referenced - Provides a testable
IHttpClientFactorybacked by the sameMock<HttpMessageHandler>used byHttpClientResolver - Caches one
HttpClientper client name: callingCreateClientwith the same name returns the same instance, different names return distinct instances - Existing
SetupHttp*andVerifyHttp*helpers apply to all named clients, since they share the same testable handler
public class CatalogService
{
private readonly HttpClient _client;
public CatalogService(IHttpClientFactory httpClientFactory)
{
_client = httpClientFactory.CreateClient("catalog");
}
public Task<HttpResponseMessage> GetProductAsync(string id)
=> _client.GetAsync($"/products/{id}");
}
[TestClass]
public class CatalogServiceTests
{
[TestMethod]
public async Task Test_WithHttpClientFactory()
{
// Arrange
AutoMocker mocker = new();
mocker.WithHttpClientFactory();
mocker.SetupHttpGet("/products/1")
.ReturnsHttpResponse(HttpStatusCode.OK, """{"id": "1"}""");
// Act
var service = mocker.CreateInstance<CatalogService>();
var response = await service.GetProductAsync("1");
// Assert
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}The generator creates:
public static AutoMocker WithHttpClientFactory(this AutoMocker mocker)
{
// Registers a resolver that provides IHttpClientFactory
// Returns mocker for fluent chaining
}The WithHttpClientFactory() method:
- Inserts a resolver after the
CacheResolverin the resolver chain - When
IHttpClientFactoryis requested, the resolver returns a testable factory implementation - Calling
CreateClient(name)on that factory looks up (or creates) anHttpClientfor the given name in an internalConcurrentDictionary<string, HttpClient>, keyed with ordinal string comparison - Every cached
HttpClientwraps the sameMock<HttpMessageHandler>used byHttpClientResolver, so requests made through any named client are visible to the existingSetupHttp*/VerifyHttp*helpers
This ensures any class depending on IHttpClientFactory receives a working implementation, without requiring a full dependency injection container, and without needing separate setup per client name.
[TestMethod]
public void Test_SameNameReturnsSameClient()
{
AutoMocker mocker = new();
mocker.WithHttpClientFactory();
var factory = mocker.Get<IHttpClientFactory>();
var first = factory.CreateClient("catalog");
var second = factory.CreateClient("catalog");
Assert.AreSame(first, second);
}[TestMethod]
public void Test_DifferentNamesReturnDifferentClients()
{
AutoMocker mocker = new();
mocker.WithHttpClientFactory();
var factory = mocker.Get<IHttpClientFactory>();
var catalog = factory.CreateClient("catalog");
var orders = factory.CreateClient("orders");
Assert.AreNotSame(catalog, orders);
}[TestMethod]
public void Test_CombinedExtensions()
{
AutoMocker mocker = new();
mocker.WithHttpClientFactory()
.WithFakeLogging();
var service = mocker.CreateInstance<CatalogService>();
// Verify both HTTP and logging behavior
}You can disable this generator using an MSBuild property in your test project's .csproj file:
<PropertyGroup>
<EnableMoqAutoMockerHttpClientFactoryGenerator>false</EnableMoqAutoMockerHttpClientFactoryGenerator>
</PropertyGroup><Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<!-- Disable HTTP Client Factory Extension Generator -->
<EnableMoqAutoMockerHttpClientFactoryGenerator>false</EnableMoqAutoMockerHttpClientFactoryGenerator>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Moq.AutoMock" Version="4.0.1" />
</ItemGroup>
</Project>- Verify your project references
Microsoft.Extensions.Http - Check that the generator is not disabled in your
.csproj - Rebuild the project to trigger generator execution
- Ensure you're using the
Moq.AutoMocknamespace
- Make sure you call
WithHttpClientFactory()before callingCreateInstance<T>() - Verify the service under test accepts
IHttpClientFactory(not a concreteHttpClient)
- Call
WithHttpClientFactory()early in your test setup, before creating instances - Use
SetupHttpGet/SetupHttpPost/etc. as usual — they apply to all named clients since they share the same testable handler - Combine with
HttpClientResolver's directHttpClientsupport when a class depends on both a concreteHttpClientandIHttpClientFactory