上QQ阅读APP看书,第一时间看更新
Writing a failing unit test
Here is the unit test you need to add to RetCalcSpec:
"RetCalc.simulatePlan" should {
"calculate the capital at retirement and the capital after death" in {
val (capitalAtRetirement, capitalAfterDeath) =
RetCalc.simulatePlan(
interestRate = 0.04 / 12,
nbOfMonthsSaving = 25 * 12, nbOfMonthsInRetirement = 40 * 12,
netIncome = 3000, currentExpenses = 2000,
initialCapital = 10000)
capitalAtRetirement should === (541267.1990)
capitalAfterDeath should === (309867.5316)
}
}
Select the call to simulatePlan, and hit Alt + Enter to let IntelliJ create the function for you in RetCalc. It should have the following signature:
def simulatePlan(interestRate: Double,
nbOfMonthsSavings: Int, nbOfMonthsInRetirement: Int,
netIncome: Int, currentExpenses: Int, initialCapital:
Double) : (Double, Double) = ???
Now compile the project with cmd + F9, and run RetCalcSpec. It should fail since the simulatePlan function must return two values. The simplest way of modeling the return type is to use Tuple2. In Scala, a tuple is an immutable data structure which holds several objects of different types. The number of objects contained in a tuple is fixed. It is akin to a case class, which does not have specific names for its attributes. In type theory, we say that a tuple or a case class is a product type.