Skip to content

Commit 4aeff4b

Browse files
committed
ContainerAutoData
1 parent 65895c7 commit 4aeff4b

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.IO.Abstractions;
2+
using Autofac;
3+
using FluentAssertions;
4+
using Noggog;
5+
using Noggog.Testing.AutoFixture;
6+
7+
namespace CSharpExt.UnitTests.AutoFixture;
8+
9+
public class ContainerAutoDataTests
10+
{
11+
public class TypicalModule : Module
12+
{
13+
protected override void Load(ContainerBuilder builder)
14+
{
15+
builder.RegisterType<Something>()
16+
.AsImplementedInterfaces().SingleInstance();
17+
}
18+
}
19+
20+
public interface ISomething
21+
{
22+
}
23+
24+
public class Something : ISomething
25+
{
26+
27+
}
28+
29+
[Theory, ContainerAutoData(typeof(TypicalModule))]
30+
public void Typical(ISomething something, int i)
31+
{
32+
something.Should().BeOfType<Something>();
33+
}
34+
35+
[Theory, ContainerAutoData(typeof(TypicalModule))]
36+
public void Missing(IFileSystem missing, int i)
37+
{
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using AutoFixture;
2+
using AutoFixture.Xunit2;
3+
4+
namespace Noggog.Testing.AutoFixture;
5+
6+
public class ContainerAutoData : AutoDataAttribute
7+
{
8+
public ContainerAutoData(
9+
Type ContainerType,
10+
TargetFileSystem FileSystem = TargetFileSystem.Fake,
11+
bool OmitAutoProperties = false)
12+
: base(() =>
13+
{
14+
var ret = new Fixture();
15+
ret.Customize(new DefaultCustomization(FileSystem));
16+
ret.OmitAutoProperties = OmitAutoProperties;
17+
ret.Customize(new ContainerAutoDataCustomization(ContainerType));
18+
return ret;
19+
})
20+
{
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Autofac;
2+
using Autofac.Core;
3+
using AutoFixture;
4+
using AutoFixture.Kernel;
5+
6+
namespace Noggog.Testing.AutoFixture;
7+
8+
public class ContainerAutoDataCustomization : ICustomization
9+
{
10+
private readonly IContainer _container;
11+
12+
public ContainerAutoDataCustomization(Type module)
13+
{
14+
var builder = new ContainerBuilder();
15+
builder.RegisterModule((IModule)Activator.CreateInstance(module)!);
16+
_container = builder.Build();
17+
}
18+
19+
public void Customize(IFixture fixture)
20+
{
21+
fixture.Customizations.Add(new ContainerSpecimenBuilder(_container));
22+
fixture.Register<IContainer>(() => _container);
23+
}
24+
25+
class ContainerSpecimenBuilder : ISpecimenBuilder
26+
{
27+
private readonly IContainer _container;
28+
private readonly HashSet<Type> _blacklist = new();
29+
30+
public ContainerSpecimenBuilder(IContainer container)
31+
{
32+
_container = container;
33+
}
34+
35+
public object Create(object request, ISpecimenContext context)
36+
{
37+
if (request is Type t && !_blacklist.Contains(t))
38+
{
39+
try
40+
{
41+
return _container.Resolve(t);
42+
}
43+
catch (Exception e)
44+
{
45+
_blacklist.Add(t);
46+
return new NoSpecimen();
47+
}
48+
}
49+
50+
return new NoSpecimen();
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)