Skip to content

Instantly share code, notes, and snippets.

@johnallers
Created February 5, 2012 04:59
Show Gist options
  • Save johnallers/1742936 to your computer and use it in GitHub Desktop.
Save johnallers/1742936 to your computer and use it in GitHub Desktop.
Tests I threw together to verify behavior of Unity.
namespace GenericInterfacesWithUnity
{
using Microsoft.Practices.Unity;
using NUnit.Framework;
[TestFixture]
public class UnityWithRegularInterfaceTests
{
private UnityContainer container;
[SetUp]
public void SetUp()
{
container = new UnityContainer();
container.RegisterType<IFooRepository, FooRepository>();
}
[Test]
public void ShouldResolveObjectUsingInterface()
{
Assert.IsNotNull(container.Resolve<IFooRepository>());
}
[Test]
public void ShouldPerformCtorInjectionOnCtorUsingInterface()
{
Assert.IsNotNull(container.Resolve<FooRepositoryDependent>());
}
[Test]
[ExpectedException(typeof(ResolutionFailedException))]
public void ShouldThrowWhenDoingCtorInjectionOnCtorUsingLessDerivedGenericInterface()
{
container.Resolve<GenericFooRepositoryDependent>();
}
}
[TestFixture]
public class UnityWithGenericInterfaceTests
{
private UnityContainer container;
[SetUp]
public void SetUp()
{
container = new UnityContainer();
container.RegisterType<IGenericRepository<Foo>, FooRepository>();
}
[Test]
public void ShouldResolveObjectUsingGenericInterface()
{
Assert.IsNotNull(container.Resolve<IGenericRepository<Foo>>());
}
[Test]
public void ShouldPerformCtorInjectionOnCtorUsingGenericInterface()
{
Assert.IsNotNull(container.Resolve<IGenericRepository<Foo>>());
}
[Test]
[ExpectedException(typeof(ResolutionFailedException))]
public void ShouldThrowWhenDoingCtorInjectionOnCtorUsingMoreDerivedInterface()
{
container.Resolve<IFooRepository>();
}
}
public interface IGenericRepository<T> { }
public interface IFooRepository : IGenericRepository<Foo> { }
public class GenericRepository<TContext, TType> : IGenericRepository<TType> { }
public class FooRepository : GenericRepository<EfContext, Foo>, IFooRepository { }
public class EfContext { }
public class Foo { }
public class FooRepositoryDependent
{
public FooRepositoryDependent(IFooRepository fooRepo)
{
}
}
public class GenericFooRepositoryDependent
{
public GenericFooRepositoryDependent(IGenericRepository<Foo> cardRepo)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment