LoginSignup
8
7

More than 5 years have passed since last update.

【Specs2】DataTables,Tablesのススメ

Posted at

Specs2のDataTablesは複数の引数パラメータのパターンでテストしたいなんて時に便利です。
サンプルは以下となります。

PrefectureListSpec.scala
class PrefectureListSpec
  extends Specification
  with DataTables {

  "サンプルのリストを取得する" in {
    "offset" | "limit" | "size" |
      0 ! 0 ! 0 |
      0 ! 2 ! 2 |
      0 ! 3 ! 3 |
      0 ! 4 ! 3 |
      1 ! 0 ! 0 |
      1 ! 2 ! 2 |
      1 ! 3 ! 2 |
      2 ! 1 ! 1 |
      3 ! 1 ! 0 |> { (offset, limit, size) =>
      val list = PrefectureRepository.listBy(offset, limit)
      list.size must_== size
    }
  }
}

ただし、DataTablesはString型使う時は!使うとコンパイルが通らなくなります。
代わりに!!を使う必要があります。
理由はよく分かりませんが、内部衝突してしまうとのこと。
なのでその場合はTablesトレイトをミックスインすると!でもコンパイルが通ります。
サンプルは以下です。

PrefectureSpec.scala
class PrefectureSpec
  extends Specification
  with Tables {

  "サンプルを登録する" in {
    "識別子" | "都道府県" | "エイリアス" |
      1 ! "東京" ! "tokyo" |
      2 ! "大阪" ! "osaka" |> { (identifier: Int, prefecture: String, alias: String) =>
      PrefectureRepository.store(identifier, prefecture, alias)
      PrefectureRepository.resolveBy(identifier) must_== Prefecture(identifier, prefecture, alias)
    }
  }
}

だったら全部Tablesでいいじゃん。って思いますよね。はい。
私も明確な理由を見つけられませんが、Tablesの中身が

Tables.scala
trait Tables extends DataTables with NoBangExamples

といった感じでNoBangExamplesをミックスインされているんで、String型が不要な時はDataTablesを使おうかといった感じにしてます。。

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7