I have a class with multiple protected methods, properties and fields. Let's say this is the class:
public class UntestedClass
{
protected int field;
protected int Method() => field + 5;
}
I want to test the class but I have to make all members of that class accessible to my test methods. There are two ways that I know of:
- Subclass UntestedClass, override protected methods with public ones and add methods or properties to access protected fields. I do not like this way as it is too much tedious work.
public class MyTestedClass : UntestedClass
{
public int Field {get => base.field; set => base.field = value; } // Now accessible as public
public new int Method() => base.Method(); // Now accessible as public
}
- Inherit UntestedClass in my Unit test class. This approach takes care of tedious work but it makes it impossible to test different constructors.
[TestClass]
public class TestsOfTests : UntestedClass
{
[TestMethod]
public void MethodTest()
{
base.field = 1; // Assign to protected field
var result = base.Method();// Invoke protected method
.... and so on ...
Is there a better way? Ideally, I would love to have something like this:
var testable = new MakeEverythingPublic<UntestedClass>();
testable.field = 42;
I do not believe this is possible but maybe there is a third way that is better than the two choices I listed above?