Let's say I have a project that is a class library. I have a class in that library and this class has some methods that are used inside the class only. Like this:
public class MyClass
{
public void MyPublicMethod
{
int k
// do something ...
int z = MyInternalMethod(k);
// do something else ...
}
internal int MyInternalMethod(int i)
{
// do something ...
}
}
Now I want to write unit tests for these methods. I would create a "Unit Tests" project, reference the nunit from it and write something like this
[TestFixture]
public class UnitTests
{
private MyClass myClass;
[SetUp]
public void SetupTest
{
myClass = new MyClass();
}
[Test]
public void TestMyInternalMethod
{
int z = 100;
int k = myClass.MyInternalMethod(z); //CAN NOT DO THIS!
Assert.AreEqual(k, 100000);
}
[TearDown]
public void TearDown
{
myClass = null;
}
}
Of course, I can not do this, because of the MyInternalMethod scope. What would be the proper way to handle this situation?
Guess it depends on your idea of what a unit is, right? I generally write unit tests for the accessible interface and ignore the private stuff. I've worked with people who will make private things protected (java) for unit test access. I really dislike that approach because it sacrifices the cleanness of the class design for test access.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With