在Specs2测试块中添加消息

时间:2015-01-12 11:47:21

标签: scala unit-testing specs2

我是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"它应该失败。从控制台屏幕看,没有"你好测试"消息,所以似乎嵌套in块没有被执行。

更新#3:

根据埃里克编辑的答案,我编辑了更新#2中的代码:

"DataServiceTest" >> {
    "my test" >> {

        "hello test" >> {    // this block is not executed
            "hello" !== "hello"
            success
        }

        "world" === "world"
        success
    }
}

同样的结果,"hello test"嵌套块没有被执行。

3 个答案:

答案 0 :(得分:0)

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

在指南中寻找“期望”。

答案 1 :(得分:0)

  1. Spec2将显示如果满足条件,您的测试将通过。不会生成任何消息,通常这就足够了。如果你的测试套件变得足够大,那么积极的消息就会变得很多。无论如何,你对失败更感兴趣。
  2. 默认情况下,Spec2会生成清除消息,并且将使用变量名称,实际值以及它应该是什么。但如果这还不够好,您可以设置自定义消息。如何做到这一点可以在this stackoverflow answer

    中看到

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

    1. 我会重构两个不同的测试:
    2. "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 {
            ...
          }
        }
      }
      

答案 2 :(得分:0)

基于一个" 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
    }
 }