Using this generic base class:
public abstract class Logic<U> where U : class
{
protected U m_provider;
public Logic(U provider)
{
m_provider = provider;
}
}
I'm trying to create a base test class for unit test:
public class LogicBaseTest<T, U> where T : Logic <U>, new() where U: class
{
protected T m_logic;
protected U m_provider;
[OneTimeSetUp]
public virtual void OneTimeSetup()
{
m_provider = (U)Substitute.For<IInterface>();
m_logic = new T(m_provider);
}
}
It complains on the constructor, it requests for the new() constrain but when I add it then it complains that the constructor cannot take parameters.
I could add a method to populate the provider but I'm wondering whether it could be done in the constructor.
where T : Logic<U>, new()you're saying "So, whatever type T is, it must inherit fromLogicand the generic parameter must beU. Also,Tneeds to have a public parameterless constructor" but yourLogic<U>class alone already violates thenew()constraint, the solution is either remove thenew()constraint or add a public parameterless constructor toLogic