From 877ca325d79013c0ff5900f82e1f53aa2ba09732 Mon Sep 17 00:00:00 2001 From: Jake Ginnivan Date: Sat, 28 Sep 2013 15:42:15 +0100 Subject: [PATCH 1/2] Added some tests around the autofac conventions --- .../CanResolveAllRegisteredServices.cs | 13 +++++++--- .../TestStack.ConventionTests.Autofac.csproj | 3 ++- .../CanResolveAllRegisteredServicesTests.cs | 23 +++++++++++++++++ ...HaveDependenciesWithLesserLifetimeTests.cs | 25 +++++++++++++++++++ .../Autofac/TestTypes/Bar.cs | 7 ++++++ .../Autofac/TestTypes/Foo.cs | 7 ++++++ .../Autofac/TestTypes/IBar.cs | 6 +++++ .../Autofac/TestTypes/IFoo.cs | 4 +++ .../TestStack.ConventionTests.Tests.csproj | 14 +++++++++++ .../packages.config | 1 + 10 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 TestStack.ConventionTests.Tests/Autofac/CanResolveAllRegisteredServicesTests.cs create mode 100644 TestStack.ConventionTests.Tests/Autofac/ServicesShouldOnlyHaveDependenciesWithLesserLifetimeTests.cs create mode 100644 TestStack.ConventionTests.Tests/Autofac/TestTypes/Bar.cs create mode 100644 TestStack.ConventionTests.Tests/Autofac/TestTypes/Foo.cs create mode 100644 TestStack.ConventionTests.Tests/Autofac/TestTypes/IBar.cs create mode 100644 TestStack.ConventionTests.Tests/Autofac/TestTypes/IFoo.cs diff --git a/TestStack.ConventionTests.Autofac/CanResolveAllRegisteredServices.cs b/TestStack.ConventionTests.Autofac/CanResolveAllRegisteredServices.cs index 9955da4..e917611 100644 --- a/TestStack.ConventionTests.Autofac/CanResolveAllRegisteredServices.cs +++ b/TestStack.ConventionTests.Autofac/CanResolveAllRegisteredServices.cs @@ -21,12 +21,17 @@ public void Execute(AutofacRegistrations data, IConventionResultContext result) .SelectMany(r => r.Services.OfType().Select(s => s.ServiceType).Union(GetGenericFactoryTypes(data, r))) .Distinct(); - var failingTypes = new List(); + var failingTypes = new List(); foreach (var distinctType in distinctTypes) { - object resolvedInstance; - if (!container.TryResolve(distinctType, out resolvedInstance)) - failingTypes.Add(distinctType); + try + { + container.Resolve(distinctType); + } + catch (DependencyResolutionException e) + { + failingTypes.Add(e.Message); + } } result.Is("Can resolve all types registered with Autofac", failingTypes); diff --git a/TestStack.ConventionTests.Autofac/TestStack.ConventionTests.Autofac.csproj b/TestStack.ConventionTests.Autofac/TestStack.ConventionTests.Autofac.csproj index 704dc50..2716e57 100644 --- a/TestStack.ConventionTests.Autofac/TestStack.ConventionTests.Autofac.csproj +++ b/TestStack.ConventionTests.Autofac/TestStack.ConventionTests.Autofac.csproj @@ -9,8 +9,9 @@ Properties TestStack.ConventionTests.Autofac TestStack.ConventionTests.Autofac - v4.5 + v4.0 512 + true diff --git a/TestStack.ConventionTests.Tests/Autofac/CanResolveAllRegisteredServicesTests.cs b/TestStack.ConventionTests.Tests/Autofac/CanResolveAllRegisteredServicesTests.cs new file mode 100644 index 0000000..3d9a786 --- /dev/null +++ b/TestStack.ConventionTests.Tests/Autofac/CanResolveAllRegisteredServicesTests.cs @@ -0,0 +1,23 @@ +namespace TestStack.ConventionTests.Tests.Autofac +{ + using global::Autofac; + using NUnit.Framework; + using TestStack.ConventionTests.Autofac; + using TestStack.ConventionTests.Tests.Autofac.TestTypes; + + [TestFixture] + public class CanResolveAllRegisteredServicesTests + { + [Test] + public void ConventionFailsWhenContainerRegistrationCannotBeResolved() + { + var containerBuilder = new ContainerBuilder(); + containerBuilder.RegisterType().As(); + var container = containerBuilder.Build(); + + var data = new AutofacRegistrations(container.ComponentRegistry); + + Assert.Throws(()=>Convention.Is(new CanResolveAllRegisteredServices(container), data)); + } + } +} \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/Autofac/ServicesShouldOnlyHaveDependenciesWithLesserLifetimeTests.cs b/TestStack.ConventionTests.Tests/Autofac/ServicesShouldOnlyHaveDependenciesWithLesserLifetimeTests.cs new file mode 100644 index 0000000..b3c9b25 --- /dev/null +++ b/TestStack.ConventionTests.Tests/Autofac/ServicesShouldOnlyHaveDependenciesWithLesserLifetimeTests.cs @@ -0,0 +1,25 @@ +namespace TestStack.ConventionTests.Tests.Autofac +{ + using global::Autofac; + using NUnit.Framework; + using TestStack.ConventionTests.Autofac; + using TestStack.ConventionTests.Tests.Autofac.TestTypes; + + [TestFixture] + public class ServicesShouldOnlyHaveDependenciesWithLesserLifetimeTests + { + [Test] + public void ConventionShouldFailForTransientInjectectedIntoSingleton() + { + var containerBuilder = new ContainerBuilder(); + containerBuilder.RegisterType().As().SingleInstance(); + containerBuilder.RegisterType().As(); + + var container = containerBuilder.Build(); + + var convention = new ServicesShouldOnlyHaveDependenciesWithLesserLifetime(); + var autofacRegistrations = new AutofacRegistrations(container.ComponentRegistry); + Assert.Throws(() => Convention.Is(convention, autofacRegistrations)); + } + } +} \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/Autofac/TestTypes/Bar.cs b/TestStack.ConventionTests.Tests/Autofac/TestTypes/Bar.cs new file mode 100644 index 0000000..31f0e73 --- /dev/null +++ b/TestStack.ConventionTests.Tests/Autofac/TestTypes/Bar.cs @@ -0,0 +1,7 @@ +namespace TestStack.ConventionTests.Tests.Autofac.TestTypes +{ + public class Bar : IBar + { + + } +} \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/Autofac/TestTypes/Foo.cs b/TestStack.ConventionTests.Tests/Autofac/TestTypes/Foo.cs new file mode 100644 index 0000000..35598ca --- /dev/null +++ b/TestStack.ConventionTests.Tests/Autofac/TestTypes/Foo.cs @@ -0,0 +1,7 @@ +namespace TestStack.ConventionTests.Tests.Autofac.TestTypes +{ + public class Foo : IFoo + { + public Foo(IBar bar){} + } +} \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/Autofac/TestTypes/IBar.cs b/TestStack.ConventionTests.Tests/Autofac/TestTypes/IBar.cs new file mode 100644 index 0000000..3f44e1f --- /dev/null +++ b/TestStack.ConventionTests.Tests/Autofac/TestTypes/IBar.cs @@ -0,0 +1,6 @@ +namespace TestStack.ConventionTests.Tests.Autofac.TestTypes +{ + public interface IBar + { + } +} \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/Autofac/TestTypes/IFoo.cs b/TestStack.ConventionTests.Tests/Autofac/TestTypes/IFoo.cs new file mode 100644 index 0000000..3381bcc --- /dev/null +++ b/TestStack.ConventionTests.Tests/Autofac/TestTypes/IFoo.cs @@ -0,0 +1,4 @@ +namespace TestStack.ConventionTests.Tests.Autofac.TestTypes +{ + public interface IFoo {} +} \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/TestStack.ConventionTests.Tests.csproj b/TestStack.ConventionTests.Tests/TestStack.ConventionTests.Tests.csproj index 1c22501..3f7bfd3 100644 --- a/TestStack.ConventionTests.Tests/TestStack.ConventionTests.Tests.csproj +++ b/TestStack.ConventionTests.Tests/TestStack.ConventionTests.Tests.csproj @@ -36,6 +36,9 @@ ..\packages\ApprovalUtilities.3.0.01\lib\net35\ApprovalUtilities.dll + + ..\packages\Autofac.3.1.1\lib\net40\Autofac.dll + ..\packages\NSubstitute.1.6.1.0\lib\NET40\NSubstitute.dll @@ -51,6 +54,12 @@ + + + + + + @@ -72,6 +81,10 @@ {D5A0D078-C660-4654-8A14-DDC816BEBC54} TestAssembly + + {a747fd64-5338-4572-879d-a9deb00ebd56} + TestStack.ConventionTests.Autofac + {955B0236-089F-434D-BA02-63A1E24C2B7C} TestStack.ConventionTests @@ -89,6 +102,7 @@ +