Play + Scala测试Slick数据库

时间:2018-01-04 17:16:11

标签: scala playframework tdd slick

我正在尝试为使用Play 2.6和Scala 2.12的项目创建一个测试类。我已经导入了最新的lib:

libraryDependencies += guice
libraryDependencies += evolutions
libraryDependencies += jdbc
libraryDependencies += filters

libraryDependencies += "com.h2database" % "h2" % "1.4.194"
libraryDependencies += "com.typesafe.play" %% "anorm" % "2.5.3"
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "3.1.0" % Test
libraryDependencies += "org.scala-lang" % "scala-actors" % "2.10.0-M7" % "test"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.4"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.4" % "test"

libraryDependencies += "com.typesafe.play" %% "play-slick" % "3.0.0"
libraryDependencies += "com.typesafe.play" %% "play-slick-evolutions" % "3.0.0"
libraryDependencies += "org.xerial" % "sqlite-jdbc" % "3.19.3"

我的编译器说最坏的lib中不存在ShouldMatchers。

class RackRepositorySpec extends PlaySpec with GuiceOneAppPerTest with Injecting {

  val database = Databases(
    driver = "org.sqlite.JDBC",
    url = "jdbc:sqlite:development.db",
    name = "default",
    config = Map(
      "username" -> "",
      "password" -> ""
    )
  )
  val guice = new GuiceInjectorBuilder()
    .overrides(bind[Database].toInstance(database))
    .injector()
  val defaultDbProvider = guice.instanceOf[DatabaseConfigProvider]

  def beforeAll() = Evolutions.applyEvolutions(database)
  def afterAll() = {
    // Evolutions.cleanupEvolutions(database)
    database.shutdown()
  }

  Evolution(
    1,
    "create table test (id bigint not null, name varchar(255));",
    "drop table test;"
  )
}

我得到了Scalatest的最后一个版本,但似乎这个类不再存在了。我正在关注此示例:https://dzone.com/articles/getting-started-play-21-scala 有没有人有另一个例子为Slick内存数据库构建Scala测试?

[info] RackRepositorySpec:
[info] models.RackRepositorySpec *** ABORTED ***
[info]   com.google.inject.ConfigurationException: Guice configuration errors:
[info] 
[info] 1) No implementation for play.api.db.slick.DatabaseConfigProvider was bound.
[info]   while locating play.api.db.slick.DatabaseConfigProvider
[info] 
[info] 1 error
[info]   at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1045)
[info]   at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1004)
[info]   at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1054)
[info]   at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:409)
[info]   at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:404)
[info]   at play.api.inject.ContextClassLoaderInjector.$anonfun$instanceOf$2(Injector.scala:117)
[info]   at play.api.inject.ContextClassLoaderInjector.withContext(Injector.scala:126)
[info]   at play.api.inject.ContextClassLoaderInjector.instanceOf(Injector.scala:117)
[info]   at models.RackRepositorySpec.<init>(RackRepositorySpec.scala:26)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

亲切的问候, 菲利普

2 个答案:

答案 0 :(得分:0)

ShouldMatchers在版本2中已弃用,已在版本3中删除。请改用MatchersMustMatchers

请参阅release notes

答案 1 :(得分:0)

我确实在我的一个Scala项目中使用了Slick,并将Postgress作为我的数据库。对于目的的单元测试,我在内存数据库中创建了一个测试h2,它在单元测试期间填充,并在测试完成后拆除。

您可以在此处查看示例:

https://github.com/joesan/plant-simulator/blob/master/test/com/inland24/plantsim/services/database/PowerPlantDBServiceSpec.scala

我还想做的是进行某种自动化,其中iI想要填充h2数据文件并将其永久保留用于下一轮的单元测试。