LoginSignup
1
1

More than 3 years have passed since last update.

Specs2でインメモリDBを用いたテストを行う場合につまづいたこと(slickを利用)

Last updated at Posted at 2015-04-07

In Memory DBを用いてDBを利用した際に困ったことをメモ。
BeforeExample, AfterExampleを使って、事前処理にテストデータの作成を、事後処理にテストデータの破棄を行おうとしました。例えば


  def before = new WithApplication(FakeApplication(additionalConfiguration = inMemoryDatabase(options=Map("MODE" -> "MySQL")))){

    DB withSession{implicit session =>
      slickDataHandler.insert(data)
    }
  }

  def after = new WithApplication(FakeApplication(additionalConfiguration = inMemoryDatabase(options=Map("MODE" -> "MySQL")))){

    DB withSession{implicit session =>
      slickDataHandler.filter(_.id === data.id).delete
    }
  }

のような感じ。この時、beforeを出るとデータがなくなっていることがわかり、そもそもテストデータを事前処理で入れるということが出来なかった。何かいい手が無いか探り中。


[追記]
https://www.playframework.com/documentation/2.1.0/ScalaTest
に書かれている下記コードのような感じでいけるのかも。試してみる。

abstract class WithDbData extends WithApplication {
  override def around[T](t: => T)(implicit evidence: (T) => Result) = super.around {
    prepareDbWithData() 
    t
  }
}

"Computer model" should {

  "be retrieved by id" in new WithDbData {
       // your test code
  }
  "be retrieved by email" in new WithDbData {
       // your test code
  }
}
1
1
1

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
1
1