我很难在一个案例类上的匹配器之间建立一个or关系,这种关系是在给定类的不同方法/字段上匹配的。
我知道我可以用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)
}发布于 2015-08-18 13:17:04
这并不完美,但你可以使用should matchPattern
d should matchPattern {
case x:Element if x.count > 0 =>
case x:Element if x.value != "" =>
}https://stackoverflow.com/questions/32072339
复制相似问题