最大规模的匹配选项

Scalatest matching options

我有这个简单的测试:

test("transform /home into Array(/home)") {
    val path = "/home"
    val expected: Option[Array[String]] = Some(Array("/home"))
    val actual: Option[Array[String]] = luceneService.buildCategoryTree(path)
    actual shouldEqual expected
}

我遇到了这个失败:

Some(Array("/home")) did not equal Some(Array("/home"))

怎么会这样?

据我了解,docs 声明我应该能够在测试中使用选项

如果我将测试更改为

actual.get shouldEqual expected.get

它通过了

最晚的docs

有一节说:

You can work with options using ScalaTest's equality, empty, defined, and contain syntax. For example, if you wish to check whether an option is None, you can write any of:

所以你的测试(抱歉我没有 de lucerne 对象),我也认为是一些 thing wrong when using arrays

Unfortunately, the current implementation is not able to "properly" understand Array equality if arrays are within another container such as Set[Array[Int]]. For example, I would have expected the following test to pass instead of throwing a TestFailedException:

进口org.scalatest._

class SetSuite extends FunSuite with Matchers {

  test("transform /home into Array(/home)") {
    val path = "/home"
    val expected: Option[Array[String]] = Some(Array("/home"))
    val actual: Option[Array[String]] = Some(Array(path))
    actual shouldEqual expected
  }
}

[info] SetSuite:
[info] - transform /home into Array(/home) *** FAILED ***
[info]   Some(Array("/home")) did not equal Some(Array("/home")) (TestScalaTest.scala:9)
[info] Run completed in 335 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0
[info] *** 1 TEST FAILED ***
[error] Failed tests:
[error]     SetSuite
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 11 s, completed Mar 17, 2016 12:26:25 AM

所以为了测试让我们使用

import org.scalatest._

class SetSuite extends FunSuite with Matchers {

  test("transform /home into Array(/home)") {
    val path = "/home"
    val expected: Option[Array[String]] = Some(Array("/home"))
    val actual: Option[Array[String]] = Some(Array(path))
    actual should contain (Array("/home"))
  }
}

[info] SetSuite:
[info] - transform /home into Array(/home)
[info] Run completed in 201 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 2 s, completed Mar 17, 2016 12:33:01 AM

匹配器似乎存在错误。 使用 Seq 而不是 Array 有效:

val expected = Some(Seq("/home"))
val actual = luceneService.buildCategoryTree(path).map(_.toSeq)
actual shouldEqual expected

这是 scalatest 的数组问题。如果您将数组转换为向量或列表

,则相同的测试工作得很好