Skip to content

Commit

Permalink
Merge pull request #41 from JakeGinnivan/AutofacConventions2
Browse files Browse the repository at this point in the history
Autofac conventions2
  • Loading branch information
JakeGinnivan committed Sep 28, 2013
2 parents 6b16512 + b94234f commit 12a5c62
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ConventionTests.proj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Test;Publish" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Test" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Root>$(MSBuildProjectDirectory)\</Root>
<BuildPlatform Condition="$(BuildPlatform) == ''">Any CPU</BuildPlatform>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ public void Execute(AutofacRegistrations data, IConventionResultContext result)
.SelectMany(r => r.Services.OfType<TypedService>().Select(s => s.ServiceType).Union(GetGenericFactoryTypes(data, r)))
.Distinct();

var failingTypes = new List<Type>();
var failingTypes = new List<string>();
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TestStack.ConventionTests.Autofac</RootNamespace>
<AssemblyName>TestStack.ConventionTests.Autofac</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Foo>().As<IFoo>();
var container = containerBuilder.Build();

var data = new AutofacRegistrations(container.ComponentRegistry);

Assert.Throws<ConventionFailedException>(()=>Convention.Is(new CanResolveAllRegisteredServices(container), data));
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Foo>().As<IFoo>().SingleInstance();
containerBuilder.RegisterType<Bar>().As<IBar>();

var container = containerBuilder.Build();

var convention = new ServicesShouldOnlyHaveDependenciesWithLesserLifetime();
var autofacRegistrations = new AutofacRegistrations(container.ComponentRegistry);
Assert.Throws<ConventionFailedException>(() => Convention.Is(convention, autofacRegistrations));
}
}
}
7 changes: 7 additions & 0 deletions TestStack.ConventionTests.Tests/Autofac/TestTypes/Bar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TestStack.ConventionTests.Tests.Autofac.TestTypes
{
public class Bar : IBar
{

}
}
7 changes: 7 additions & 0 deletions TestStack.ConventionTests.Tests/Autofac/TestTypes/Foo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TestStack.ConventionTests.Tests.Autofac.TestTypes
{
public class Foo : IFoo
{
public Foo(IBar bar){}
}
}
6 changes: 6 additions & 0 deletions TestStack.ConventionTests.Tests/Autofac/TestTypes/IBar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TestStack.ConventionTests.Tests.Autofac.TestTypes
{
public interface IBar
{
}
}
4 changes: 4 additions & 0 deletions TestStack.ConventionTests.Tests/Autofac/TestTypes/IFoo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace TestStack.ConventionTests.Tests.Autofac.TestTypes
{
public interface IFoo {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<Reference Include="ApprovalUtilities">
<HintPath>..\packages\ApprovalUtilities.3.0.01\lib\net35\ApprovalUtilities.dll</HintPath>
</Reference>
<Reference Include="Autofac">
<HintPath>..\packages\Autofac.3.1.1\lib\net40\Autofac.dll</HintPath>
</Reference>
<Reference Include="NSubstitute">
<HintPath>..\packages\NSubstitute.1.6.1.0\lib\NET40\NSubstitute.dll</HintPath>
</Reference>
Expand All @@ -51,6 +54,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Autofac\CanResolveAllRegisteredServicesTests.cs" />
<Compile Include="Autofac\ServicesShouldOnlyHaveDependenciesWithLesserLifetimeTests.cs" />
<Compile Include="Autofac\TestTypes\Bar.cs" />
<Compile Include="Autofac\TestTypes\Foo.cs" />
<Compile Include="Autofac\TestTypes\IBar.cs" />
<Compile Include="Autofac\TestTypes\IFoo.cs" />
<Compile Include="ConventionAssertionClassTests.cs" />
<Compile Include="CsvReportTests.cs" />
<Compile Include="MvcConventions.cs" />
Expand All @@ -72,6 +81,10 @@
<Project>{D5A0D078-C660-4654-8A14-DDC816BEBC54}</Project>
<Name>TestAssembly</Name>
</ProjectReference>
<ProjectReference Include="..\TestStack.ConventionTests.Autofac\TestStack.ConventionTests.Autofac.csproj">
<Project>{a747fd64-5338-4572-879d-a9deb00ebd56}</Project>
<Name>TestStack.ConventionTests.Autofac</Name>
</ProjectReference>
<ProjectReference Include="..\TestStack.ConventionTests\TestStack.ConventionTests.csproj">
<Project>{955B0236-089F-434D-BA02-63A1E24C2B7C}</Project>
<Name>TestStack.ConventionTests</Name>
Expand All @@ -89,6 +102,7 @@
<ItemGroup>
<None Include="Resources\ProjectFileWithInvalidSqlScriptFile.txt" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
1 change: 1 addition & 0 deletions TestStack.ConventionTests.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<packages>
<package id="ApprovalTests" version="3.0.01" targetFramework="net40" />
<package id="ApprovalUtilities" version="3.0.01" targetFramework="net40" />
<package id="Autofac" version="3.1.1" targetFramework="net40" />
<package id="NSubstitute" version="1.6.1.0" targetFramework="net40" />
<package id="NUnit" version="2.6.2" targetFramework="net40" />
</packages>

0 comments on commit 12a5c62

Please sign in to comment.