Unit test complex classes with many private methods
Posted
by Simon G
on Stack Overflow
See other posts from Stack Overflow
or by Simon G
Published on 2010-04-29T15:33:21Z
Indexed on
2010/04/29
15:37 UTC
Read the original article
Hit count: 344
Hi,
I've got a class with one public method and many private methods which are run depending on what parameter are passed to the public method so my code looks something like:
public class SomeComplexClass
{
IRepository _repository;
public SomeComplexClass()
this(new Repository())
{
}
public SomeComplexClass(IRepository repository)
{
_repository = repository;
}
public List<int> SomeComplexCalcualation(int option)
{
var list = new List<int>();
if (option == 1)
list = CalculateOptionOne();
else if (option == 2)
list = CalculateOptionTwo();
else if (option == 3)
list = CalculateOptionThree();
else if (option == 4)
list = CalculateOptionFour();
else if (option == 5)
list = CalculateOptionFive();
return list;
}
private List<int> CalculateOptionOne()
{
// Some calculation
}
private List<int> CalculateOptionTwo()
{
// Some calculation
}
private List<int> CalculateOptionThree()
{
// Some calculation
}
private List<int> CalculateOptionFour()
{
// Some calculation
}
private List<int> CalculateOptionFive()
{
// Some calculation
}
}
I've thought of a few ways to test this class but all of them seem overly complex or expose the methods more than I would like. The options so far are:
Set all the private methods to internal and use [assembly: InternalsVisibleTo()]
Separate out all the private methods into a separate class and create an interface.
Make all the methods virtual and in my tests create a new class that inherits from this class and override the methods.
Are there any other options for testing the above class that would be better that what I've listed?
If you would pick one of the ones I've listed can you explain why?
Thanks
© Stack Overflow or respective owner