在 Specs2 测试块中添加消息

Adding message in a block of Specs2 test

我是 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" 嵌套块没有被执行。

您可能会发现 Specs2 指南很有帮助: https://etorreborre.github.io/specs2/guide/org.specs2.guide.Structure.html#Expectations

在指南中查找 "Expectations"。

  1. Spec2 只会显示如果满足条件,您的测试就会通过。不会生成任何消息,通常这就足够了。如果您的测试套件变得足够大,那么正面消息就会变得很多。反正你对失败更感兴趣。

Spec2 确实会默认生成明确的消息,并且会使用变量名、实际值以及它通常应该是什么。但如果这还不够好,您可以设置自定义消息。如何做到这一点可以在 this Whosebug answer

中看到

如果您仍想生成消息,可以使用打印语句和记录器。但我建议你不要这样做。

  1. 我会重构为两个不同的测试:
"DataServiceTest"
should {
  "InsertNewData should have correct basic properties" in {
    // Arrange 
    val name = "some name here"
    val description = "some description here"

    // Act
    // Assumed DataService.insertNewData method execute the insertion
    // and returns "Data" model.
    Data data = DataService.insertNewData(name, description)

    // Assert
    // Checking of equality here
    data.name === name
    data.description === description
  }

  "InsertNewData should have correct subData" in {
    // Arrange 
    val name = "some name here"
    val description = "some description here"

    // Act
    // Assumed DataService.insertNewData method execute the insertion
    // and returns "Data" model.
    Data data = DataService.insertNewData(name, description)

    // Assert
    // Assumed the "Data" model has List[SubData] property "subData"
    // and the code below is checking the 
    data.subData must have size(3)

    data.subData.foreach {
      ...
    }
  }
}

基于一个 "ACT" 调用创建小示例的简单方法是使用像这样的惰性 val:

"DataServiceTest should" >> {
  "InsertNewData" >> {
    val name = "some name here"
    val description = "some description here"

    // Assumed DataService.insertNewData method execute the insertion
    // and returns "Data" model.
    lazy val data = DataService.insertNewData(name, description)

    "name must be ok" >> {
      data.name === name
    }
    "description must be ok" >> {
      data.description === description
    }

    "subdata must be ok" >> {
       data.subData.foreach {
       ...
       }
       success
    }
 }