如何使用Slick and Play在测试中手动应用演变! 2.4 [英] How to apply manually evolutions in tests with Slick and Play! 2.4

查看:69
本文介绍了如何使用Slick and Play在测试中手动应用演变! 2.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每个测试文件的开头手动运行我的演进脚本.我正在与Play合作! 2.4和Slick 3.

I would like to manually run my evolution script at the beginning of each test file. I'm working with Play! 2.4 and Slick 3.

根据文档,该方法似乎是:

According to the documentation, the way to go seems to be:

Evolutions.applyEvolutions(database)

但是我无法获取数据库实例.在文档 play.api.db.Databases是为了获取数据库实例而导入,但是如果我尝试导入它,则会出现此错误:object Databases is not a member of package play.api.db

but I don't manage to get an instance of my database. In the documentation play.api.db.Databases is imported in order to get a database instance but if I try to import it, I get this error: object Databases is not a member of package play.api.db

如何获取数据库实例以运行演进脚本?

How can I get an instance of my database in order to run the evolution script?

,如注释中所述,这是给出错误的整个源代码:

as asked in the comments, here is the entire source code giving the error:

import models._
import org.scalatest.concurrent.ScalaFutures._
import org.scalatest.time.{Seconds, Span}
import org.scalatestplus.play._
import play.api.db.evolutions.Evolutions
import play.api.db.Databases

class TestAddressModel extends PlaySpec with OneAppPerSuite {
   lazy val appBuilder = new GuiceApplicationBuilder()
   lazy val injector = appBuilder.injector()
   lazy val dbConfProvider = injector.instanceOf[DatabaseConfigProvider]

  def beforeAll() = {
    //val database: Database = ???
    //Evolutions.applyEvolutions(database)
  }

  "test" must { 
     "test" in { } 
  } 
}

推荐答案

我终于找到了这个解决方案.我注入了Guice:

I finally found this solution. I inject with Guice:

lazy val appBuilder = new GuiceApplicationBuilder()

lazy val injector = appBuilder.injector()

lazy val databaseApi = injector.instanceOf[DBApi] //here is the important line

(您必须导入play.api.db.DBApi.)

在测试中,我只需执行以下操作(实际上,我在测试中使用了另一个数据库):

And in my tests, I simply do the following (actually I use an other database for my tests):

override def beforeAll() = {
  Evolutions.applyEvolutions(databaseApi.database("default"))
}

override def afterAll() = {
  Evolutions.cleanupEvolutions(databaseApi.database("default"))
}

这篇关于如何使用Slick and Play在测试中手动应用演变! 2.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆