5

Typesafe Slick (v 1.0.1) を使用する Play (v 2.2.0) アプリを持っています。PostgreSQL データベースをシードし、さまざまなコントローラー アクションを呼び出すテスト (specs2) を作成しようとしています。データの存在を検証します。私のテストでは、次のものがあります。

 "Countries" should {
      "initialize" in {
        running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
          AppDB.database.withSession {
            implicit session: Session =>

              AppDB.dal.create
              AppDB.dal.seedForTests

              AppDB.dal.Countries.findAll().size must be_>=(1)
          }
        }
      }

それだけで、これは正常に機能します。しかし、別のテスト アクションを追加すると、次のようになります。

  "respond to Index()" in {
    val result = controllers.Countries.index()(FakeRequest())

    status(result) must equalTo(OK)
  }

私のテストは次のメッセージで失敗します:

SQLException: Attempting to obtain a connection from a pool that has already been shutdown.

スタックトレースの関連部分は次のとおりです。

[error]     SQLException: Attempting to obtain a connection from a pool that has already been shutdown. 
[error] Stack trace of location where pool was shutdown follows:
[error]  java.lang.Thread.getStackTrace(Thread.java:1503)
[error]  com.jolbox.bonecp.BoneCP.captureStackTrace(BoneCP.java:559)
[error]  com.jolbox.bonecp.BoneCP.shutdown(BoneCP.java:161)
[error]  com.jolbox.bonecp.BoneCPDataSource.close(BoneCPDataSource.java:143)
[error]  play.api.db.BoneCPApi.shutdownPool(DB.scala:414)
[error]  play.api.db.BoneCPPlugin$$anonfun$onStop$1.apply(DB.scala:264)
[error]  play.api.db.BoneCPPlugin$$anonfun$onStop$1.apply(DB.scala:262)
[error]  scala.collection.immutable.List.foreach(List.scala:318)
[error]  play.api.db.BoneCPPlugin.onStop(DB.scala:262)
...

FakeApplication(...)AppDB.database.withSessionブロックの両方をコードの上位に移動し、val result = controllers.Countries.index(...)コードをラッパーでラップしようとしましAppDB.database.withSessionたが、まだ運がありませんでした。

方向性をありがとう。

4

1 に答える 1

2

AroundExampleDB を初期化し、テストを実行するために使用できます。

class CountriesSpec extends mutable.Specification with AroundExample {

  def around[R : AsResult](r: =>R) = 
    running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
      AppDB.database.withSession { implicit session: Session =>
        AppDB.dal.create
        AppDB.dal.seedForTests
        AppDB.dal.Countries.findAll().size must be_>=(1)
        // just AsResult(r) with the latest 2.2.3 specs2 
        AsResult.effectively(r)
      }
    }

  "Countries" should {
    "respond to Index()" in {
      val result = controllers.Countries.index()(FakeRequest())
      status(result) must equalTo(OK)
    }
  }
}
于 2013-10-10T23:27:34.110 に答える