2014-09-30 41 views
0

在此代码段中,SqlTypes.BinaryTypes是元组的(String, Int)的列表。在使用forAll时在Scala Specs2中输出列表元素作为示例

"process binary types" >> { 
    SqlTypes.BinaryTypes.foreach(sqlType => { 
    val label = sqlType._1 
    val value = sqlType._2 

    s"$label" in new ABinaryColumnHandler { 
     handler.canProcess(value) should beTrue 
    } 
    }) 

    "for all of them" in new ABinaryColumnHandler { 
    handler should processTypes(SqlTypes.BinaryTypes) 
    } 
} 

第一部分,其中所述foreach是,创建用于列表中的每个元素的新实施例,并运行简单的测试。第二部分调用自定义匹配:

def processTypes(typeList: List[(String, Int)]): Matcher[ColumnHandler] = (handler: ColumnHandler) => { 
    forall(typeList) { sqlType => 
     val value = sqlType._2 
     handler.canProcess(value) should beTrue 
    } 
} 

第一部分将不无后定义的另一个例子运行,因为foreach的回报Unit而不是ExampleFragment。我尝试了第二种方式,因为它看起来更直接,但我无法得到它的输出结构,我想要的结构是这样的(在IntelliJ中,但在SBT中看起来类似):

output in intellij

我真正想要的是Specs2输出在这两种情况下是相同的,或更喜欢

process binary types 
    for all of them 
     BINARY 
     VARBINARY 
     LONGBINARY 

我如何可以调整后者的例子运行到输出我想要的方式?

回答

1

使用>>和返回Unit的块不明确。你想创造例子还是创造期望?

// is it this? 
"several examples" >> { 
    Seq(1, 2).foreach { i => s"example $i" >> { 1 must_== 1 } } 
} 

// or that? 
"several expectations" >> { 
    Seq(1, 2).foreach { i => i must_== i } 
} 

为了解决你需要使用的那2个方法之一的模糊性:

  • examplesBlock创建实例
  • Result.unit创建期望

像这样:

class TestSpec extends org.specs2.mutable.Specification { 

    "process binary types" >> examplesBlock { 
    List("BINARY", "VARBINARY").foreach { label => 
     s"$label" in new ABinaryColumnHandler { 
     Result.unit(Seq(1, 2).foreach(i => i must_== i)) 
     } 
    } 
    } 

    trait ABinaryColumnHandler extends Scope 
} 

User Guide中有关于这两种方法的说明。

+0

感谢您的回答。我想创建示例。这与我所寻找的非常接近。完成一个具有一个examplesBlock的函数,并将类型和匹配器传递给该函数。例如。 ''处理二进制类型“>> {eachTypeShould(SqlTypes.BinaryTypes,beTrue)}' – 2014-10-01 20:13:27

相关问题