The general approach looks great, but you should add ContainerSpecimenBuilder to ResidueCollectors instead of Customizations :
fixture.ResidueCollectors.Add(new ContainerSpecimenBuilder(containerBuilder.Build()));
AutoMoqCustomization also adds a node to ResidueCollectors , so you may need to experiment a bit with a specific order to figure out how to make it behave the way you want it to behave. Order Issues .
See the AutoFixture Architecture documentation for more information on the differences between Customizations and ResidueCollectors .
A slightly simpler (and safer?) Implementation of ContainerSpecimenBuilder can simply process requests for Type instances directly, and not for SeededRequest , since almost all SeededRequest values ββare passed to requests for Type objects anyway:
internal class ContainerSpecimenBuilder : ISpecimenBuilder { private readonly IContainer container; public ContainerSpecimenBuilder(IContainer container) { this.container = container; } public object Create(object request, ISpecimenContext context) { var t = request as Type; if (t == null) return new NoSpecimen(request); var result = this.container.ResolveOptional(t); return result ?? new NoSpecimen(request); } }
source share