使用Slick和Evolutions测试Scala播放

时间:2018-07-26 11:02:39

标签: scala testing playframework

我想编写一个光滑的DAO类的测试。应隔离每个测试,并应在两次测试调用之间刷新数据库状态。我将使用Evolutions对数据库本身进行必要的更改。

我的代码是:

class FoundedItemsRepositoryTest extends CommonTest with GuiceOneAppPerSuite with ScalaFutures {
  implicit override lazy val app = new GuiceApplicationBuilder()
    .overrides(bind[Utils].to[UtilsMock])
    .overrides(bind[Mail].to[MailMock])
    .disable[Module]
    .in(Mode.Test)
    .build

  lazy val injector = app.injector
  lazy val databaseApi = injector.instanceOf[DBApi]

  override def withFixture(test: NoArgTest) = { // Define a shared fixture
    // Shared setup (run at beginning of each test)

    Evolutions.applyEvolutions(databaseApi.database("default"))

    try test()
    finally {
      // Shared cleanup (run at end of each test)
      Evolutions.cleanupEvolutions(databaseApi.database("default"))
    }
  }


  def foundedItemsRepository(implicit app: Application): FoundedItemsRepository = Application.instanceCache[FoundedItemsRepository].apply(app)

  "loadWebsites result" should " contain some records" in {
    whenReady(foundedItemsRepository.loadWebsites(w => true)) { res =>
      res.size should be > 0
    }
  }
}

我的application.conf包含:

slick.dbs {
    default {
        profile="slick.jdbc.SQLiteProfile$"
        db.driver="org.sqlite.JDBC"
        db.url="jdbc:sqlite:/home/generic/harv.db"
        db.user=harv
        db.password="harv"
    }
}

调用以错误结束:

  

java.lang.IllegalArgumentException:找不到数据库   默认

我想这与我具有 slick.dbs.default.db.url 的精巧配置有关,而不是普通的jdbc形式 db.default.url

我该如何解决?

1 个答案:

答案 0 :(得分:0)

为了构建数据库对象,我们需要将jdbc添加到库依赖项中,以便在build.sbt文件中进行测试阶段:

libraryDependencies ++= Seq(
  jdbc % Test,
  "com.typesafe.slick" %% "slick" % "3.2.1",
  "com.typesafe.play" %% "play-slick" %  "3.0.2",
  ...
)

我在运行阶段使用slick而不是jdbc,因此jdbc在未测试时是多余的-如上所列。