2015-08-18 42 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 != "" => 
} 
相關問題