I've said in the past to treat test code just like any other
code. However, there are a few reasons to break from this rule. One is the
context switching which will be caused by extracting information which is
important to a test. Let me explain what I mean with two examples.
Example Using Helper Method
[Test]
public void CalcInterestRoundsToTenthPennies()
{
decimal initialMoney = 100.00;
decimal expectedInterest = 33.333
InterestCalculator ic = GetTestInterestCalculator();
decimal actualInterest = ic.CalcInterest(initialMoney);
Assert.AreEqual(expectedInterest, actualInterest,
"CalcInterest did not round to a tenth of a penny correctly");
}
Example Without Helper Method
[Test]
public void CalcInterestRoundsToTenthPennies()
{
decimal initialMoney = 100.00;
decimal interestRate = 0.3333333333;
decimal expectedInterest = 33.333
InterestCalculator ic = new InterestCalculator(interestRate);
decimal actualInterest = ic.CalcInterest(initialMoney);
Assert.AreEqual(expectedInterest, actualInterest,
"CalcInterest did not round to a tenth of a penny correctly");
}
Notice here that you can tell a lot more about what is going
on in the second one, because you know what percentage is being used to
calculate. You cannot tell that the first one is accurate since you can't see
the percent. I recommend trying to keep the amount of values to a minimum and
if you're writing your code well you'll have few enough dependencies that you
will not have a problem. These helper methods as you can see make it so you
will need to go to another location to see what was initially created. In some
instances they are useful, but use them sparingly as they might hide details.