The following Customization passes the above test, and does what you seem to ask
public class MvcCostumization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(
new FilteringSpecimenBuilder(
new MethodInvoker(new ModestConstructorQuery()),
new ControllerSpecification()));
}
private class ControllerSpecification : IRequestSpecification
{
public bool IsSatisfiedBy(object request)
{
var t = request as Type;
if (t == null)
return false;
return typeof(ControllerBase).IsAssignableFrom(t);
}
}
}
However, that disables all properties of all MVC Controllers, including User
, HttpContext
, ModelState
, Request
, Response
, etc. You might need those in unit tests, so are you sure you want to do that?
I'll be the first person to agree that the MVC Controller
base class is one huge SRP violation, but it's a class which is given to us, so there's nothing we can do about except trying to make the best of it.
Usually, you only need to twist it a little to make AutoFixture fill most of the properties automatically.
In MVC 3 projects, this, combined with the AutoMoq extension, is sufficient to create Controller instances:
fixture.Customize<ModelBindingContext>(c => c
.Without(x => x.Model)
.Without(x => x.ModelType));
fixture.Inject(CultureInfo.CurrentCulture);