To support your open generic type, you can write a custom Specimen Builder:
public class KeyedEntityBuilder : ISpecimenBuilder
{
private readonly static MethodInfo createMethod =
typeof(KeyedEntity).GetMethod("Create");
public object Create(object request, ISpecimenContext context)
{
var t = request as Type;
if (t == null ||
!t.IsGenericType ||
t.GetGenericTypeDefinition() != typeof(KeyedEntity<>))
return new NoSpecimen(request);
var entityType = t.GetGenericArguments().Single();
var key = context.Resolve(typeof(Identifier));
var entity = context.Resolve(entityType);
return createMethod
.MakeGenericMethod(entityType)
.Invoke(null, new[] { key, entity });
}
}
(Defensive coding omitted for clarity.)
The following unit test passes:
public class Tests
{
[Fact]
public void CreateKeyedEntity()
{
var fixture = new Fixture();
fixture.ResidueCollectors.Add(new KeyedEntityBuilder());
var actual = fixture.Create<KeyedEntity<Foo>>();
Assert.NotNull(actual.Key);
Assert.NotNull(actual.Entity);
}
}
For better maintainability, you should encapsulate KeyedEntityBuilder
in a Customization.