【问题标题】:Adding message in a block of Specs2 test在 Specs2 测试块中添加消息
【发布时间】: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


    【解决方案1】:

    基于一个“ACT”调用创建小示例的一种简单方法是使用这样的惰性验证:

    "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
        }
     }
    

    【讨论】:

    • 嗨,“名称必须没问题”、“描述必须没问题”和“子数据必须没问题”块在“InsertNewData”块内吗?因为嵌套的 in 块不起作用,请参阅我的问题中的 UPDATE #2
    • 我编辑了我的答案。您可以使用>> 运算符,而不是使用嵌套的“in”块。
    • 更新#3 并不是我上面写的。在更新 #3 中,您有一段代码,这是一个示例,因为它以期望结尾。在示例的中间,您创建了另一个未执行的示例,因为 specs2 中的设计时间和执行时间是分开的。
    • 我不太明白。您能否修改示例 #3 中的代码以使其正常工作?
    • 没有真正的区别。 >> 可以在阅读 in 没有意义时使用。 should 也一样。您可以始终使用>> 并在说明中添加should(或can,或must,或...)或in,仅当它们有意义时。
    【解决方案2】:

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

    在指南中寻找“期望”。

    【讨论】:

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

      默认情况下,Spec2 确实会生成明确的消息,并将使用变量名称、实际值以及通常应该是什么。但如果这还不够好,您可以设置自定义消息。如何做到这一点可以在this stackoverflow 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 {
            ...
          }
        }
      }
      

      【讨论】:

      • 在这种情况下,我应该把Data data = DataService.insertNewData(name, description)代码放在哪里?
      • 我更新了示例,并按照 Arrange、Act、Assert 模式显式设置了测试:c2.com/cgi/wiki?ArrangeActAssert
      • 在您的示例中,Data data = DataService.insertNewData(name, description) 等一些代码是多余的或重复的。
      猜你喜欢
      • 2015-09-12
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多