Specs2を試す(3)

Unit specification

Unit specification はshould/inブロックを使ってFragments(Fragmentのリスト)を作る。
Acceptance Specification は org.specs2.Specification を継承しているのに対し、
Unit specification は org.specs2.mutable.Specification を継承している。
これは Unit Specification は、ミュータブルな変数に Fragment を追加していくことで Fragments を組み立てているから、たぶん。


一つ一つのFragment(Resultを返す)はinブロックで囲む。それをずらずら並べたやつをshouldで囲む。


書き方は違うけどやりたいことは Acceptance Specification と同じ。

  • should を ^ に変えて
  • in を ! に変えて
  • in 同士も ^ でつなぐ

で、Unit Specification -> Acceptance Specification という変換ができるね。


Unit Specification

  "The 'Hello world' string" should {
    "contain 11 characters" in {
      "Hello world" must have size(11)
    }
    "start with 'Hello'" in {
      "Hello world" must startWith("Hello")
    }
    "end with 'world'" in {
      "Hello world" must endWith("world")
    }
  }


Acceptance Specification

  def is =

  "The 'Hello world' string should" ^
    "contain 11 characters" ! {
      "Hello world" must have size(11)
    }^
    "start with 'Hello'" ! {
      "Hello world" must startWith("Hello")
    }^
    "end with 'world'" ! {
      "Hello world" must endWith("world")
    }