Play Slick:如何使用Play and Slick在内存单元测试用例中编写

时间:2017-08-19 12:17:25

标签: scala unit-testing playframework guice slick

我正在使用Play 2.6和Slick 3.2与MySql数据库。但问题是我无法使用h2数据库编写测试用例。问题是,通过使用guice,我无法在我的回购中注入DatabaseConfigProvider。以下是我的repo类实现:

class CompanyRepoImpl @Inject() (dbConfigProvider: DatabaseConfigProvider)
                                (implicit ec: ExecutionContext) extends CompanyRepo {

  val logger: Logger = LoggerFactory.getLogger(this.getClass())
  private val dbConfig = dbConfigProvider.get[JdbcProfile]

  import dbConfig._
  import profile.api._

  private val company = TableQuery[Companies]

------
}

我的测试用例类:

class CompanyRepoSpec extends AsyncWordSpec with Matchers with BeforeAndAfterAll {

  private lazy val injector: Injector = new GuiceApplicationBuilder()
    .overrides(bind(classOf[CompanyRepo]).to(classOf[CompanyRepoImpl]))
    .in(Mode.Test)
    .injector()
  private lazy val repo: CompanyRepo = injector.instanceOf[CompanyRepo]
  private lazy val dbApi = injector.instanceOf[DBApi]

  override protected def beforeAll(): Unit = {
    Evolutions.applyEvolutions(database = dbApi.database("test"))
  }

  override protected def afterAll(): Unit = {
    Evolutions.cleanupEvolutions(database = dbApi.database("test"))
  }

-----------------------
}

测试application.test.conf

play.evolutions.db.test.enabled=true
play.evolutions.autoApply=true

slick.dbs.test.profile="slick.jdbc.H2Profile$"
slick.dbs.test.db.driver="org.h2.Driver"
slick.dbs.test.db.url="jdbc:h2:mem:test;MODE=MySQL"

可能是,我的配置无法进行测试,因此,我们如何使用scala,slick和play编写测试用例。

0 个答案:

没有答案