I had to make a fix, where the line of code was removed for the second time. So I decided to write a unit test just for that.
A Class in its constructor sets a property that I want to test. This property happens to be protected so I can't access it in the unit test.
[Test]
public void Constructor_WhenCalled_ThenSomePropertyIsPopulated()
{
var vm= new SomeViewModel();
//line below doesn't compile as SomeProperty is protected
Assert.IsNotNull(vm.SomeProperty);
So I decided, in my unit test file, to extend that class (it wasn't sealed) and expose the protected property as a public getter:
public class SomeViewModelExtended : SomeViewModel
{
public SomeViewModelExtended() : base() { }
public new object SomeProperty
{
get { return base.SomeProperty; }
}
}
//now I can test
[Test]
public void Constructor_WhenCalled_ThenSomePropertyIsPopulated()
{
var vm= new SomeViewModelExtended();
Assert.IsNotNull(vm.SomeProperty);
Now there's an argument on my team that I should only be testing public interfaces among other things and that this is a total quick and dirty hack.
But isn't one of the purposes of unit tests to preserve the code base from unwanted changes? If this is totally wrong what else should do?