2015-02-17 41 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了解详细信息),但在你的情况下,有条件的财产是完全合适的。