如何将 ScalaTest "inside" 与数组一起使用?

How do I use ScalaTest "inside" with an Array?

我有一个案例 class,我正在尝试测试类似的案例 to this。变化会是这样的...

case class Record(names: Array[Name] ...)

我是 Scala 的新手,不确定这在语法上是如何工作的

请考虑以下代码:

case class Name(first: String, middle: String, last: String)
case class Record(names: Array[Name])
val rec = Record(
  Array(Name("Sally", "Anna", "Jones"), Name("Sally1", "Anna1", "Jones1"))
)

inside (rec) { case Record(nameArray) =>
  inside (nameArray) { case Array(name, name1) =>
    inside(name) {
      case Name(first, middle, last) =>
        first should be("Sally")
        middle should be("Anna")
        last should be("Jones")
    }
    inside(name1) {
      case Name(first, middle, last) =>
        first should be("Sally1")
        middle should be("Anna1")
        last should be("Jones1")
    }
  }
}

请注意,如果 case Array(name, name1) 处的名称数量与实际不同,则测试将失败。

正如Luis在评论中提到的,不建议在类情况下使用数组。如果将 Array 更改为 ListVectorArraySeq.

,此代码将同样有效