Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created December 2, 2017 15:43
Show Gist options
  • Save dcomartin/6cc581c36cee7ac0cdd79af6d3d42257 to your computer and use it in GitHub Desktop.
Save dcomartin/6cc581c36cee7ac0cdd79af6d3d42257 to your computer and use it in GitHub Desktop.
using Shouldly;
using Unity;
using Unity.Injection;
using Xunit;
namespace Tests
{
public interface IFoo { }
public class Foo : IFoo { }
public class UnityTests
{
[Fact]
public void Should_overwrite_concrete_with_injection()
{
var container = new UnityContainer();
var foo1 = new Foo();
container.RegisterType<Foo>(new InjectionFactory(x => foo1));
var foo2 = new Foo();
container.RegisterType<Foo>(new InjectionFactory(x => foo2));
var result = container.Resolve<Foo>();
result.GetHashCode().ShouldBe(foo2.GetHashCode());
}
[Fact]
public void Should_overwrite_interface_with_injection()
{
var container = new UnityContainer();
var foo1 = new Foo();
container.RegisterType<IFoo>(new InjectionFactory(x => foo1));
var foo2 = new Foo();
container.RegisterType<IFoo>(new InjectionFactory(x => foo2));
var result = container.Resolve<IFoo>();
result.GetHashCode().ShouldBe(foo2.GetHashCode());
}
[Fact]
public void Should_overwrite_interface()
{
var container = new UnityContainer();
var foo1 = new Foo();
container.RegisterType<IFoo, Foo>(new InjectionFactory(x => foo1));
var foo2 = new Foo();
container.RegisterType<IFoo>(new InjectionFactory(x => foo2));
var result = container.Resolve<IFoo>();
result.GetHashCode().ShouldBe(foo2.GetHashCode());
}
[Fact]
public void Should_resolve_injection()
{
var container = new UnityContainer();
var foo1 = new Foo();
container.RegisterType<IFoo, Foo>(new InjectionFactory(x => foo1));
var result1 = container.Resolve<IFoo>();
result1.GetHashCode().ShouldBe(foo1.GetHashCode()); // FAILS
}
[Fact]
public void Should_overwrite_interface3()
{
var container = new UnityContainer();
container.RegisterType<IFoo, Foo>();
var foo2 = new Foo();
container.RegisterType<IFoo>(new InjectionFactory(x => foo2));
var result = container.Resolve<IFoo>();
result.GetHashCode().ShouldBe(foo2.GetHashCode()); // FAILS
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment