Scalatest:如果两个匹配器之一匹配,则测试有效

Scalatest: have a test which is valid if one of both matchers matches

我很难在一个案例 class 的匹配器之间建立 or 关系,该案例匹配给定 class 的不同 methods/fields。

我知道我可以用 exists|| 来完成它,它最终会得到 Bool 但会从测试框架中删除我不想要的所有反馈。

这是我想做的一个例子:

class ExampleSpec extends FunSpec with Matchers {

  case class Element(count: Int,  value: String)

  val data : List[Element] = List(
    Element(0, "ok"),
    Element(5, "")
    Element(0,""),
    Element(1, "a")
  )

  describe("My data test") {
    data foreach {d =>
        it("valid data either has a count > 0 or the value is not empty") {
          d.count should be > 0 or d.value should not be empty // I have no idea how to get the or working
        }

    }
  }
}

我能想到的最好的事情是:

   def okishSolution(e: Element) = {
     val res = (e.count > 0 || d.value.nonEmpty)
     if (! res) { info(s"Failed: $d , did not meet requirements") }

      res should be(true)
    }

它并不完美,但您可以使用 should matchPattern

d should matchPattern {
  case x:Element if x.count > 0 =>
  case x:Element if x.value != "" =>
}