Skip to content

Instantly share code, notes, and snippets.

@skarllot
Created January 30, 2025 21:49
Show Gist options
  • Save skarllot/61867a46b20b3b0749a65e0b932dd44e to your computer and use it in GitHub Desktop.
Save skarllot/61867a46b20b3b0749a65e0b932dd44e to your computer and use it in GitHub Desktop.
AutoFixture support to ImmutableList
using System.Collections.Immutable;
using AutoFixture.Kernel;
public class ImmutableListSpecimenBuilder : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (request is not Type t)
{
return new NoSpecimen();
}
var typeArguments = t.GetGenericArguments();
if (typeArguments.Length != 1 || typeof(ImmutableList<>) != t.GetGenericTypeDefinition())
{
return new NoSpecimen();
}
dynamic list = context.Resolve(typeof(IList<>).MakeGenericType(typeArguments));
return ImmutableList.ToImmutableList(list);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment