Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test code that is highly complex behind the public interface

I'm wondering how I should be testing this sort of functionality via NUnit.

Public void HighlyComplexCalculationOnAListOfHairyObjects()
{
    // calls 19 private methods totalling ~1000 lines code + comments + whitespace
}

From reading I see that NUnit isn't designed to test private methods for philosophical reasons about what unit testing should be; but trying to create a set of test data that fully executed all the functionality involved in the computation would be nearly impossible. Meanwhile the calculation is broken down into a number of smaller methods that are reasonably discrete. They are not however things that make logical sense to be done independently of each other so they're all set as private.

like image 697
Dan Is Fiddling By Firelight Avatar asked Jan 06 '10 16:01

Dan Is Fiddling By Firelight


1 Answers

You've conflated two things. The Interface (which might expose very little) and this particular Implementation class, which might expose a lot more.

  1. Define the narrowest possible Interface.

  2. Define the Implementation class with testable (non-private) methods and attributes. It's okay if the class has "extra" stuff.

  3. All applications should use the Interface, and -- consequently -- don't have type-safe access to the exposed features of the class.

What if "someone" bypasses the Interface and uses the Class directly? They are sociopaths -- you can safely ignore them. Don't provide them phone support because they violated the fundamental rule of using the Interface not the Implementation.

like image 115
S.Lott Avatar answered Oct 07 '22 20:10

S.Lott