Specs2を試す(2)

Acceptance specification

参考: http://etorreborre.github.com/specs2/guide/org.specs2.guide.SpecStructure.html#Specification+structure

Acceptance specification では、一つ一つの'frgment'を^でつなぎ、
'fragmentのリスト(flagments)'を作るという書き方をする。

例えば下の例は

  • 1つのText fragment
  • 2つのexample fragmentから成っている。
  "this is my specification"                          ^
    "and example 1"                                   ! e1^
    "and example 2"                                   ! e2

  def e1 = success
  def e2 = success

example fragmentは
"description" ! body という形をしている

    // description                                      body
    "and example 1"                                   ! e1

bodyはResult型を返すメソッドであり、Textの部分とは分けて定義する。


bodyの命名の仕方について、ルールとかは特にないようだ。
e1, e2のように短い名前にしてもよいし、``で囲んで長い説明的な名前を付けるのもよい。


  "this is my specification"                          ^
    "and example 1"                                   ! `first example`^
    "and example 2"                                   ! `second example`

  // ``で囲む
  def `first example` = success
  def `second example` = success


descriptionを省略した書き方もできる。
長い名前を付けてたらdescriptionの部分いらないもんね。

  "this is my specification"                          ^
    `and example 1`                                   ^
    `and example 2`

  def `and example 1` = success
  def `and example 2` = success

蛇足

``で囲むやつ、よくJavaのメソッドとScalaの予約後がぶつかる場面で使うけど
こんな使いかたもできたんですね。

scala> def `な が い な ま え` = println("こういうことできるのはじめてしりました。")                                                               
な$u0020が$u0020い$u0020な$u0020ま$u0020え: Unit

scala> `な が い な ま え`    
こういうことできるのはじめてしりました。