One thing that bothers me while I'm writing unit tests of my code is if tests of methods should include validation of the outcome of other methods? Of course, public methods. Let's see a - rather trivial - example:
public int Solve(string forumla)
{
if (forumla.Contains("+"))
{
return SolveAddition(formula);
}
if (forumla.Contains("-"))
{
return SolveSubstraction(formula);
}
throw new ArgumentException();
}
public int SolveAddition(string formula)
{
var strings = formula.Split('+');
if (strings.Length != 2)
{
throw new ArgumentException();
}
int firstFactor = int.Parse(strings[0]);
int secondFactor = int.Parse(strings[0]);
return firstFactor + secondFactor;
}
In this case, should tests of Solve
methods only test control flow, or each possible scenario of SolveAddition
? Let's assume, that SolveAddition
and SolveSubstraction
have their own tests.
Using other words, did test scenarios for Solve
should contains only:
- 2+2
- 2-2
- 2:2
Or also include:
- 2+2+2+2
- 2-2-2-2
- potato - hamster
Best regards