|
|
|
@ -12,24 +12,36 @@ namespace Trash.Tests;
|
|
|
|
|
|
|
|
|
|
public record ServiceFactoryWrapper(Type Service, Action<ILifetimeScope> Instantiate);
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
[Parallelizable(ParallelScope.All)]
|
|
|
|
|
public class CompositionRootTest
|
|
|
|
|
public static class FactoryForService<TService>
|
|
|
|
|
{
|
|
|
|
|
private static class FactoryForService<TService>
|
|
|
|
|
public static ServiceFactoryWrapper WithArgs<TP1>(TP1 arg1 = default!)
|
|
|
|
|
{
|
|
|
|
|
public static ServiceFactoryWrapper WithArgs<TP1>(TP1 arg1 = default!)
|
|
|
|
|
{
|
|
|
|
|
return new ServiceFactoryWrapper(typeof(TService),
|
|
|
|
|
c => c.Resolve<Func<TP1, TService>>().Invoke(arg1));
|
|
|
|
|
}
|
|
|
|
|
return new ServiceFactoryWrapper(typeof(TService),
|
|
|
|
|
c => c.Resolve<Func<TP1, TService>>().Invoke(arg1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
[Parallelizable(ParallelScope.All)]
|
|
|
|
|
public class CompositionRootTest
|
|
|
|
|
{
|
|
|
|
|
private static readonly List<ServiceFactoryWrapper> FactoryTests = new()
|
|
|
|
|
{
|
|
|
|
|
FactoryForService<IGitRepository>.WithArgs("path")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
[TestCaseSource(typeof(CompositionRootTest), nameof(FactoryTests))]
|
|
|
|
|
public void Service_requiring_factory_should_be_instantiable(ServiceFactoryWrapper service)
|
|
|
|
|
{
|
|
|
|
|
var act = () =>
|
|
|
|
|
{
|
|
|
|
|
using var container = CompositionRoot.Setup();
|
|
|
|
|
service.Instantiate(container);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
act.Should().NotThrow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private sealed class ConcreteTypeEnumerator : IEnumerable
|
|
|
|
|
{
|
|
|
|
|
private readonly IContainer _container;
|
|
|
|
@ -44,28 +56,19 @@ public class CompositionRootTest
|
|
|
|
|
return _container.ComponentRegistry.Registrations
|
|
|
|
|
.SelectMany(x => x.Services)
|
|
|
|
|
.OfType<TypedService>()
|
|
|
|
|
.Where(x => FactoryTests.All(y => y.Service != x.ServiceType))
|
|
|
|
|
.Select(x => x.ServiceType)
|
|
|
|
|
.Distinct()
|
|
|
|
|
.Except(FactoryTests.Select(x => x.Service))
|
|
|
|
|
.Where(x => x.FullName == null || !x.FullName.StartsWith("Autofac."))
|
|
|
|
|
.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestCaseSource(typeof(CompositionRootTest), nameof(FactoryTests))]
|
|
|
|
|
public void Service_requiring_factory_should_be_instantiable(ServiceFactoryWrapper service)
|
|
|
|
|
{
|
|
|
|
|
var act = () =>
|
|
|
|
|
{
|
|
|
|
|
using var container = CompositionRoot.Setup();
|
|
|
|
|
service.Instantiate(container);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
act.Should().NotThrow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestCaseSource(typeof(ConcreteTypeEnumerator))]
|
|
|
|
|
public void Service_should_be_instantiable(Service service)
|
|
|
|
|
public void Service_should_be_instantiable(Type service)
|
|
|
|
|
{
|
|
|
|
|
using var container = CompositionRoot.Setup();
|
|
|
|
|
container.Invoking(c => c.ResolveService(service))
|
|
|
|
|
container.Invoking(c => c.Resolve(service))
|
|
|
|
|
.Should().NotThrow()
|
|
|
|
|
.And.NotBeNull();
|
|
|
|
|
}
|
|
|
|
|