У меня есть простая функция разделения Scala, например:
class ScalaFunction {
/**
* This is simple mathematics division function. Example: numerator = 20 and denominator = 4 equals 5 (result).
* @param numerator also called dividend
* @param denominator also called divisor
* @return called quotient and it calculates how many times can we divide numerator by denominator
*/
def divide(numerator: Double, denominator: Double): Double = denominator / numerator
}
Очевидно, что denominator
и numerator
поменялись местами. Я хочу написать модульный тест с фреймворком scalatest с org.scalatest.WordSpecLike
style и DSL org.scalatest.MustMatchers
. В конце класс Scala Spec должен выглядеть так:
class ScalaFunctionSpec extends WordSpecLike with MustMatchers {
"A division function" when {
val scalaFunction = new ScalaFunction()
"divide 7 by 0" must {
"result in exception or infinity" in {
// how to check for infinity or division by 0?
// scalaFunction.divide(7, 0) must equal(?inf?)
}
}
}
}
Как проверить/проверить бесконечность/деление на 0 в рамках scalatest?
Спасибо!
Лучший
Infinity
, вы можете использоватьDouble.PositiveInfinity
. В случае деления с плавающей запятой на ноль вы не получите исключения. Вы получите/ by zero
ArithmeticException
при делении целых чисел на 0. - person Patryk Ćwiek   schedule 24.01.2014