2015-02-17 56 views
1

我必須測試一個無循環圖並總是檢查字符串是否不同不是非常有用(它會引發異常)。必須有更好的解決方案,但我無法想出它,而且我在specs2文檔中迷失了方向。 這是代碼的示例:Specs2 + Scalacheck使用不同的字符串生成元組

"BiDirectionalEdge" should { 
"throw an Error for the wrong DirectedEdges" in prop { 
    (a :String, b :String, c :String, d :String) => 
    val edge1 = createDirectedEdge(a, b, c) 
    val edge2 = createDirectedEdge(c, b, d) 
    new BiDirectionalEdge(edge1, edge2) must throwA[InvalidFormatException] or(a mustEqual d) 
} 

如果a和c都是一樣的,createDirectedEdge將拋出異常(ⅰ具有該行爲不同的測試)。

回答

1

是的,還有更好的方法 - 這正是條件屬性的用途。只需添加您的病情,然後==>

"BiDirectionalEdge" should { 
    "throw an Error for the wrong DirectedEdges" in prop { 
    (a: String, b: String, c: String, d: String) => (a != c) ==> 
     val edge1 = createDirectedEdge(a, b, c) 
     val edge2 = createDirectedEdge(c, b, d) 
     new BiDirectionalEdge(edge1, edge2) must 
     throwA[InvalidFormatException] or(a mustEqual d) 
    } 
} 

如果條件很可能會經常失敗,你應該採取不同的方法(見the ScalaCheck guide瞭解詳細信息),但在你的情況下,有條件的財產是完全合適的。