【发布时间】:2015-01-12 11:47:21
【问题描述】:
我是 Specs2 的新手。我阅读了 specs2 文档,但这有点令人困惑。不知道有没有可能。
所以我的 Specs2 测试代码大致是这样的:
"DataServiceTest" should {
"InsertNewData" in {
val name = "some name here"
val description = "some description here"
// Assumed DataService.insertNewData method execute the insertion
// and returns "Data" model.
Data data = DataService.insertNewData(name, description)
// Checking of equality here
data.name === name
data.description === description
// Assumed the "Data" model has List[SubData] property "subData"
// and the code below is checking the List[SubData]
data.subData.foreach {
...
}
success
}
}
1.有没有办法为这些部分提供信息?
data.name === name
data.description === description
类似于"Checking data's name" in { data.name === name }。因此,无论测试成功还是失败,都会在输出屏幕上显示消息。
2. 有没有办法通过提供这样的文本消息来对“InsertNewData”内的子代码进行分组:
"DataServiceTest" should {
"InsertNewData" in {
val name = "some name here"
val description = "some description here"
// Assumed DataService.insertNewData method execute the insertion
// and returns "Data" model.
Data data = DataService.insertNewData(name, description)
"Checking basic properties of Data" in {
// Checking of equality here
data.name === name
data.description === description
}
"Checking subData" in {
// Assumed the "Data" model has List[SubData] property "subData"
// and the code below is checking the subData
data.subData must have size(3)
data.subData.foreach {
...
}
}
success
}
}
更新:
根据这里的一个答案,我尝试了这个:
"Checking of equality" ! e1
def e1 = {
data.name === name
data.description === description
failure
}
它没有工作,因为它应该失败。但是测试结果全部通过了。
更新 #2:
我做了一些嵌套in块的实验:
"DataServiceTest" should {
"my test" in {
"hello test" in { // this block is not executed
"hello" !== "hello"
success
}
"world" === "world"
success
}
}
结果是成功,但应该因为"hello" !== "hello"而失败。从控制台屏幕看,没有“hello test”消息,因此嵌套的in 块似乎没有被执行。
更新 #3:
根据 eric 编辑的答案,我在更新 #2 中编辑了代码:
"DataServiceTest" >> {
"my test" >> {
"hello test" >> { // this block is not executed
"hello" !== "hello"
success
}
"world" === "world"
success
}
}
同样的结果,"hello test" 嵌套块没有被执行。
【问题讨论】:
标签: scala unit-testing specs2