【发布时间】:2020-04-11 18:49:44
【问题描述】:
我是 scala 的新手,我正在尝试找出测试以下过程的最佳方法。
我有一个从构造函数参数中获取数字列表的类。该类支持对列表的各种操作,一些操作可能依赖于其他操作的输出。但是每个选项都应该只根据需要执行计算,并且最多应该执行一次。构造函数中不应进行任何计算。
类定义示例 .
输入列表:列表[Int]。
x:返回 InputList 中所有元素的平方的向量。
y:返回 x 中所有元素的总和。
z:返回 y 的平方根。
至于类实现,我想我能够想出一个合适的解决方案,但现在我不知道如何测试依赖的操作树的计算只执行一次。
类实现方法#1:
class Operations(nums: List[Int]) {
lazy val x: List[Int] = nums.map(n => n*n)
lazy val y: Int = x.sum
lazy val z: Double = scala.math.sqrt(y)
}
这是我的第一个方法,我有信心可以完成这项工作,但无法弄清楚如何正确测试它,所以我决定添加一些辅助方法来确认它们被称为只是一个
类实现方法#2:
class Ops(nums: List[Int]) {
def square(numbers: List[Int]): List[Int] = {
println("calling square function")
numbers.map(n => n*n)
}
def sum(numbers: List[Int]): Int = {
println("calling sum method")
numbers.sum
}
def sqrt(num: Int): Double = {
println("calling sqrt method")
scala.math.sqrt(num)
}
lazy val x: Vector[Double] = square(nums)
lazy val y: Double = sum(x)
lazy val z: Double = sqrt(y)
}
我现在可以确认每个方法的每个依赖方法在必要时只调用一次。
现在我该如何为这些进程编写测试。我看过一些关于 mockito 的帖子并查看了文档,但找不到我想要的东西。我看了以下内容:
展示了如何测试一个函数是否被调用一次,然后如何测试其他依赖函数是否被调用? http://www.scalatest.org/user_guide/testing_with_mock_objects#mockito
看起来很有希望,但我不知道语法:
https://github.com/mockito/mockito-scala
我要执行的示例测试
var listoperations:Ops = new Ops(List(2,4,4))
listoperations.y // confirms 36 is return, confirms square and sum methods were called just once
listoperations.x // confirms List(4,16,16) and confirms square method was not called
listoperations.z // confirms 6 is returned and sqrt method called once and square and sum methods were not called.
【问题讨论】:
-
我要退后一步,你为什么要测试这些方法相互调用多少次?
-
假设您必须处理一个非常大的列表,并且如果您调用更高级别的函数,您的代码应该能够重用您所做的任何计算。例如,如果您先调用 z,然后调用 x,则代码不应进行任何重新计算。我希望我的测试用例始终检查此行为以保证此性能
标签: scala mockito scalatest scalamock