dd

Scala Specs2 测试入门教程(5): Fragments API 简介

jerry Scala 2015年11月25日 收藏

前面的例子

import org.specs2._

class HelloWorldAcceptanceSpec extends Specification { def is = s2"""

 This is a specification to check the 'Hello world' string

 The 'Hello world' string should
   contain 11 characters                                         $e1
   start with 'Hello'                                            $e2
   end with 'world'                                              $e3
                                                                 """

  def e1 = "Hello world" must have size(11)
  def e2 = "Hello world" must startWith("Hello")
  def e3 = "Hello world" must endWith("world")
}

实际上是使用Fragment API,我们使用Fragment API重写什么例子,它是一系列Fragment对象通过^运算符连接而成:

import org.specs2._

class HelloWorldAcceptanceSpec extends Specification { def is =

 "This is a specification to check the 'Hello world' string" ^
 "The 'Hello world' string should" ^
   "contain 11 characters"                                         ! e1 ^
   "start with 'Hello'"                                            ! e2 ^
   "end with 'world'"                                              ! e3


  def e1 = "Hello world" must have size(11)
  def e2 = "Hello world" must startWith("Hello")
  def e3 = "Hello world" must endWith("world")
}

其中的Example对象的格式为 ?description? ! body. body返回一个Result对象,当你直接使用Fragment API使用^运算符构建Text和Example对象时有些规则需要注意,这些规则影响到结果的显示格式。

规则

spec2的结果显示的布局缺省情况是自动完成,其显示和源码显示类似。
其显示规则如下:

  • 当一个Example跟在一个Text对象后面,它缩进显示。
  • 两个连续的Example对象,缩进层次相同。
  • 但一个Text对象跟在一个Example对象后面时,意味着你想显示一个“辅助文字(子文字)”,因此这个Text对象缩进一级。

dd