When your test project references Microsoft.Extensions.Options, this generator creates a WithOptions<T>() extension method for AutoMocker that simplifies testing classes that depend on IOptions<T>.
- Automatically generates when
Microsoft.Extensions.Optionsis referenced - Provides fluent API for configuring options in tests
- Sets up all necessary options infrastructure (
IOptionsMonitor,IOptionsSnapshot, etc.)
using Microsoft.Extensions.Options;
public class MyService
{
public IOptions<MySettings> Settings { get; }
public MyService(IOptions<MySettings> settings)
{
Settings = settings;
}
}
public class MySettings
{
public int Number { get; set; }
public string Required { get; set; }
}
[TestClass]
public class MyServiceTests
{
[TestMethod]
public void Test_WithOptions()
{
AutoMocker mocker = new();
// Use the generated WithOptions extension method
mocker.WithOptions<MySettings>(options =>
{
options.Number = 42;
options.Required = "test value";
});
MyService service = mocker.CreateInstance<MyService>();
Assert.AreEqual(42, service.Settings.Value.Number);
Assert.AreEqual("test value", service.Settings.Value.Required);
}
}The generator creates:
public static AutoMocker WithOptions<TClass>(this AutoMocker mocker, Action<TClass>? configure = null)
where TClass : class
{
// Sets up IOptions<T>, IOptionsMonitor<T>, IOptionsSnapshot<T>, etc.
// Applies the configure action to the options instance
}The WithOptions<T>() method sets up the complete options infrastructure in your AutoMocker instance:
- Creates an
IConfigureOptions<T>with your configuration delegate - Sets up
IOptionsMonitorCache<T>usingOptionsCache<T> - Sets up
IOptionsFactory<T>usingOptionsFactory<T> - Sets up
IOptionsMonitor<T>usingOptionsMonitor<T> - Sets up
IOptionsSnapshot<T>usingOptionsManager<T> - Creates an
IOptions<T>instance with your configured values - Registers the configured options instance directly
This means you can inject any of these types into your classes under test:
IOptions<T>IOptionsSnapshot<T>IOptionsMonitor<T>IOptionsFactory<T>- The configuration type
Tdirectly
You can disable this generator using an MSBuild property in your test project's .csproj file:
<PropertyGroup>
<EnableMoqAutoMockerOptionsGenerator>false</EnableMoqAutoMockerOptionsGenerator>
</PropertyGroup><Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<!-- Disable Options Extension Generator -->
<EnableMoqAutoMockerOptionsGenerator>false</EnableMoqAutoMockerOptionsGenerator>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Moq.AutoMock" Version="3.5.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
</ItemGroup>
</Project>- Verify
Microsoft.Extensions.Optionsis referenced in your test project - 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're calling
WithOptions<T>()before creating your instance - Verify the configuration delegate is setting the properties correctly
- Check that you're using
Settings.Valueto access the configured options