Play Slick: How to inject DbConfigProvider in tests

寵の児 提交于 2020-01-20 08:09:08

问题


I am using Play 2.5.10, Play-slick 2.0.2, and my activator-generated project comes with scalatest and code like this:

class TestSpec extends PlaySpec with OneAppPerSuite {...}

I managed to test routes/Actions; now I would test DAO methods on a lower level. I searched the web and SO for a solution, and could not find any that is still up-to-date. A DAO signature is like this:

class TestDAO @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends HasDatabaseConfigProvider[JdbcProfile]

so I need to pass it the dbConfigProvider thing. For some reason I can't inject the provider into the tests like we do in controllers (no error, tests just won't run):

class TestSpec @Inject()(dbConfigProvider: DatabaseConfigProvider) extends PlaySpec with OneAppPerSuite {...}

The Play-Slick docs say we can alternatively use a global lookup

val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)

but it won't work directly because

There is no started application

and link to an example project doing that:

class TestDAOSpec extends Specification {
  "TestDAO" should {
    "work as expected" in new WithApplicationLoader {   // implicit 'app'
      val app2dao = Application.instanceCache[TestDAO].apply(app)

but I could never find the WithApplicationLoader. Instead, there seems to be a WithApplication:

class TestDAOSpec extends Specification {
  "TestDAO" should {
    "work as expected" in new WithApplication() {   // implicit 'app'
      val app2dao = Application.instanceCache[TestDAO].apply(app)

but then I get

Type mismatch: expected a play.api.Application, got: play.Application.

At this point I lost hope.

How can I test a DAO?

N.B. I don't need to switch databases for testing (I handle this via config), I just want to access the default database in tests.


回答1:


You can use:

lazy val appBuilder: GuiceApplicationBuilder = new GuiceApplicationBuilder().in(Mode.Test) 
lazy val injector: Injector = appBuilder.injector()
lazy val dbConfProvider: DatabaseConfigProvider = injector.instanceOf[DatabaseConfigProvider]


来源:https://stackoverflow.com/questions/42021022/play-slick-how-to-inject-dbconfigprovider-in-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!