我正在尝试为返回包含数组的类的元组的函数编写单元测试。ScalaTest深度比较最佳实践
简单的assert(out === expectedOut)
或out should be(expectedOut)
不会比较由于数组而导致的LHS和RHS上的类的内容。在ScalaTest中有没有一种简单的方法可以做到这一点?
我看过自定义匹配器,但我不确定这是否是我尝试做的最好的方法。因此,来自专家经验的任何信息将不胜感激。
编辑: 下面是似乎其中不是这种情况的情况下:
object Utils {
case class Product(id: Int, prices: Array[Int])
def getProductInfo(id: Int, prices: Array[Int]): Option[Product] = {
val sortedPrices = prices.sortWith(_ < _)
Some(Product(id, sortedPrices))
}
}
---
import org.scalatest._
import Utils._
class DataProcessorSpec extends FlatSpec with Matchers with OptionValues {
val id = 12345
val priceList = Array(10,20,30)
val prod = Utils.getProductInfo(id, priceList)
val expectedProd = Some(Utils.Product(id, priceList))
"A DataProcessorSpec" should "return the correct product information" in {
prod should be(expectedProd)
}
}
测试失败,因为sortWith
导致创建一个新的数组,并因此指向不同的存储位置,据我所知。
您能否提供一段代码?你可以像Tzach指出的那样使用匹配器,但我不完全确定你在这里试图维护什么。 – manub