最新的匹配选项

时间:2016-03-16 21:38:57

标签: scala scalatest

我有这个简单的测试:

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

传递

3 个答案:

答案 0 :(得分:3)

在最简洁的docs

有一节说:

  

您可以使用ScalaTest的相等,空,定义,   并包含语法。例如,如果您想检查是否   选项为无,您可以写任何:

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

  

不幸的是,目前的实施无法“正确”   如果数组在另一个容器中,则理解数组相等   如Set [Array [Int]]。例如,我本来期望以下   测试传递而不是抛出TestFailedException:

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 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

答案 1 :(得分:1)

看起来匹配器存在错误。 使用Seq代替Array有效:

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

答案 2 :(得分:0)

这是阵列的最新问题。如果将数组转换为矢量或列表

,则相同的测试工作正常