2015-08-18 41 views
2

我很难在给定类的不同方法/字段上匹配的case类的匹配器之间有or关系。Scalatest:如果两个匹配器中的一个匹配,则测试有效

我知道我可以用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) 
    } 
+0

您是否阅读过文档和/或? http://www.scalatest.org/user_guide/using_matchers#logicalExpressions – Daenyth

+0

是的,因为这种情况没有记录在那里,我问这个问题。 –

+1

我明白你的意思了。他们之前已经对github门票做出了响应,可能会提交一份。您可以使用自定义组合器来完成此任务。 – Daenyth

回答

0

这不是完美的,但你可以使用should matchPattern

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