Skip to content

Instantly share code, notes, and snippets.

@reharik
Last active December 15, 2020 07:58
Show Gist options
  • Save reharik/6888093 to your computer and use it in GitHub Desktop.
Save reharik/6888093 to your computer and use it in GitHub Desktop.
Autofixture
public class test
{
[Theory, TestData]
public void make_this_populate_a_controller( TestController SUT)
{
var actionResult = SUT.Get("hello");
var x = "";
}
}
public class TestController:ApiController
{
public TestController()
{
}
public string Get(string input)
{
return "";
}
}
public class TestDataAttribute : AutoDataAttribute
{
public TestDataAttribute()
: base(new Fixture().Customize(new MvcCustomization())
.Customize(new FakeHttpContentCustomization())
.Customize(new ContainerCustomization(TestStructureMapBootstrapper.Bootstrap())))
{
}
}
public class MvcCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customize<ControllerContext>(c => c.OmitAutoProperties());
}
}
public class FakeHttpContentSpecimenBuilder : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
var type = request as Type;
if (type != typeof(HttpContent))
return new NoSpecimen(request);
return new FakeHttpContent("Fake");
}
}
public class FakeHttpContent : HttpContent
{
public string Content { get; set; }
public FakeHttpContent(string content)
{
Content = content;
}
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
byte[] byteArray = Encoding.ASCII.GetBytes(Content);
await stream.WriteAsync(byteArray, 0, Content.Length);
}
protected override bool TryComputeLength(out long length)
{
length = Content.Length;
return true;
}
}
public class ContainerCustomization : ICustomization
{
private readonly IContainer container;
public ContainerCustomization(IContainer container)
{
this.container = container;
}
public void Customize(IFixture fixture)
{
fixture.ResidueCollectors.Add(new ContainerSpecimenBuilder(this.container));
}
}
public class ContainerSpecimenBuilder : ISpecimenBuilder
{
private readonly IContainer _container;
public ContainerSpecimenBuilder(IContainer container)
{
_container = container;
}
public IContainer Container { get; set; }
public object Create(object request, ISpecimenContext context)
{
var type = request as Type;
if (type == null || !type.IsInterface)
return new NoSpecimen(request);
return _container.GetInstance(type);
}
}
public class TestStructureMapBootstrapper
{
public static IContainer Bootstrap()
{
new TestStructureMapBootstrapper().Start();
GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(ObjectFactory.Container);
return ObjectFactory.Container;
}
private void Start()
{
ObjectFactory.Initialize(x => x.Scan(y =>
{
y.AssemblyContainingType<TestController>();
y.WithDefaultConventions();
}));
}
}
public class StructureMapDependencyResolver : IDependencyResolver
{
public StructureMapDependencyResolver(IContainer container)
{
_container = container;
}
public object GetService(Type serviceType)
{
if (serviceType.IsAbstract || serviceType.IsInterface)
{
return _container.TryGetInstance(serviceType);
}
else
{
return _container.GetInstance(serviceType);
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _container.GetAllInstances<object>()
.Where(s => s.GetType() == serviceType);
}
private readonly IContainer _container;
}
public class FakeHttpContentCustomization: ICustomization
{
public void Customize(IFixture fixture)
{
fixture.ResidueCollectors.Add(new FakeHttpContentSpecimenBuilder());
}
}
@moodmosaic
Copy link

Can you provide the code for the FakeHttpContentCustomization class? (It is the only type missing.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment