如何从 DatabaseConfigProvider 将演化应用于测试数据库
How to apply evolutions to test database from DatabaseConfigProvider
我想考dao (play 2.6.21, play-slick 3.0.3
):
@Singleton
class MyDao @Inject()(@NamedDatabase("default") protected val dbConfigProvider: DatabaseConfigProvider)
(implicit val sc: ExecutionContext)
extends HasDatabaseConfigProvider[JdbcProfile] {
...
}
我将测试数据库的数据库配置放在测试资源中:
slick.dbs.default {
profile = "slick.jdbc.PostgresProfile$"
endpoint = "localhost:12345"
schema = "my_test_schemma"
db {
driver = "org.postgresql.Driver"
url = "jdbc:postgresql://"${slick.dbs.default.endpoint}"/"${slick.dbs.default.schema}""
}
}
在测试中,我想应用进化:
class MyDaoSpec extends FlatSpec with GuiceFakeApplicationFactory {
implicit lazy val app: Application = fakeApplication()
"dao" should "something" in {
val conf = app.injector.instanceOf[DatabaseConfigProvider]
val db = app.injector.instanceOf[Database]
Evolutions.applyEvolutions(db)
val dao = app.injector.instanceOf[MyDao]
//tests...
}
}
但它给我错误:
Guice configuration errors for instanceOf[Database]
:
1) No implementation for play.api.db.Database was bound. while
locating play.api.db.Database
那么我如何访问数据库并对其应用进化?
我发现,我需要使用 EvolutionComponents
,但不明白如何使用它
我的错误是,我添加了以下依赖项:
libraryDependencies ++= Seq(evolutions, jdbc)
但正确的是:
"com.typesafe.play" %% "play-slick-evolutions" % "3.0.3",
测试没有特别的步骤,就在配置里:
play.evolutions.enabled = true
play.evolutions.autoApply = true
我想考dao (play 2.6.21, play-slick 3.0.3
):
@Singleton
class MyDao @Inject()(@NamedDatabase("default") protected val dbConfigProvider: DatabaseConfigProvider)
(implicit val sc: ExecutionContext)
extends HasDatabaseConfigProvider[JdbcProfile] {
...
}
我将测试数据库的数据库配置放在测试资源中:
slick.dbs.default {
profile = "slick.jdbc.PostgresProfile$"
endpoint = "localhost:12345"
schema = "my_test_schemma"
db {
driver = "org.postgresql.Driver"
url = "jdbc:postgresql://"${slick.dbs.default.endpoint}"/"${slick.dbs.default.schema}""
}
}
在测试中,我想应用进化:
class MyDaoSpec extends FlatSpec with GuiceFakeApplicationFactory {
implicit lazy val app: Application = fakeApplication()
"dao" should "something" in {
val conf = app.injector.instanceOf[DatabaseConfigProvider]
val db = app.injector.instanceOf[Database]
Evolutions.applyEvolutions(db)
val dao = app.injector.instanceOf[MyDao]
//tests...
}
}
但它给我错误:
Guice configuration errors for
instanceOf[Database]
:1) No implementation for play.api.db.Database was bound. while locating play.api.db.Database
那么我如何访问数据库并对其应用进化?
我发现,我需要使用 EvolutionComponents
,但不明白如何使用它
我的错误是,我添加了以下依赖项:
libraryDependencies ++= Seq(evolutions, jdbc)
但正确的是:
"com.typesafe.play" %% "play-slick-evolutions" % "3.0.3",
测试没有特别的步骤,就在配置里:
play.evolutions.enabled = true
play.evolutions.autoApply = true