using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.Unity;
using ResolvingWithPolicyInjection.DemoClasses;
namespace ResolvingWithPolicyInjection.Tests
{
[TestClass]
public class UnityContainerExtensionMethodsTest
{
[TestMethod]
public void should_resolve_ITestInterface_to_policy_enabled_TestConcreteImplementation_explicit()
{
// Arrange
var container = new UnityContainer();
container.RegisterType<ITestInterface, TestConcreteImplementation>();
// Act
var resolvedWithPolicyInjection = container.ResolveWithPolicyInjection<ITestInterface>();
// Assert
Assert.IsNotNull(resolvedWithPolicyInjection);
Assert.AreEqual("TEST", resolvedWithPolicyInjection.ReturnTest());
}
[TestMethod]
public void should_resolve_ITestInterface_to_policy_enabled_TestConcreteImplementation_implicitly()
{
// Arrange
var container = new UnityContainer();
// Act
container.RegisterTypeWithPolicyInjection<ITestInterface, TestConcreteImplementation>();
// Resolved the dependent class - which should do the resolving of ITestInterface to TestConcreteImplementation
var resolved = container.Resolve<DependentConcreteImplementation>();
// Assert
Assert.IsNotNull(resolved);
Assert.AreEqual("TEST", resolved.KörBeroende());
}
}
}
namespace ResolvingWithPolicyInjection.DemoClasses
{
/// <summary>
/// This class is dependent of ITestInterface
/// </summary>
public class DependentConcreteImplementation
{
private readonly ITestInterface _beroende;
/// <summary>
/// Constructor dependency - ITestInterface that will be resolved by Unity
/// </summary>
public DependentConcreteImplementation([Dependency]ITestInterface dependency)
{
_beroende = dependency;
}
public string KörBeroende()
{
return _beroende.ReturnTest();
}
}
public interface ITestInterface
{
string ReturnTest();
}
/// <summary>
/// This class implements ITestInterface
/// </summary>
public class TestConcreteImplementation : ITestInterface
{
public string ReturnTest()
{
return "TEST";
}
}
}