I have IInterface with appropriate realization Realization which is registered in IUnityContainer (Unity framework):
public interface IInterface
{
void Foo();
}
public class Realization : IInterface
{
public void Foo() => Console.WriteLine("Test");
}
public class BaseFixture
{
protected IUnityContainer Container { get; set; }
[OneTimeSetUp]
public void OneTimeSetUp()
{
Container = new UnityContainer()
.RegisterType<IInterface, Realization>();
}
}
I have Nunit TestFixture class in which I try to resolve the dependency in two ways:
Constructor:
[TestFixture]
public class MyTestClass1: BaseFixture
{
public IInterface MyProp { get; set; }
public MyTestClass1(IInterface instance)
{
MyProp = instance;
}
[Test]
public void MyTest1()
{
MyProp.Foo();
}
}
Property:
[TestFixture]
public class MyTestClass2 : BaseFixture
{
[Dependency]
public IInterface MyProp { get; set; }
[Test]
public void MyTest2()
{
MyProp.Foo();
}
}
In the first case(constructor injection) I have the next exception on the runtime:
OneTimeSetUp: No suitable constructor was found
In the second case (property injection) the property is not initialized and has null
value.
I would appreciate if anybody can advice the solution to use property or constructor injection. The only solution I've googled is: https://github.com/kalebpederson/nunit.dependencyinjection, but not sure that it is the best one.
Thanks.