尝试为Scala中的Play Web应用程序创建Slick数据库的单元测试

时间:2018-02-02 12:52:20

标签: scala unit-testing playframework slick

我正在努力尝试使用Scala为我的Play网络应用程序配置单元测试。我使用的是Play 2.6和Scala 2.11.8。当我使用数据库配置并执行sbt test时,我在控制台上收到错误No implementation for play.api.db.slick.DatabaseConfigProvider was bound。所以我将展示如何设置我的网络应用程序,也许有人可以指出错误。只是为了这个缘故,Web应用程序运行良好。它只是我无法创建的数据库的单元测试。

build.sbt

import play.sbt.PlayImport._

name := """crypto-miners-demo"""    
version := "1.0-SNAPSHOT"    
lazy val root = (project in file(".")).enablePlugins(PlayScala)    
scalaVersion := "2.11.8"

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

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 += "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"

libraryDependencies += "org.apache.spark" %% "spark-core" % "2.2.0"
libraryDependencies += "org.apache.spark" %% "spark-sql" % "2.2.0"

dependencyOverrides += "com.fasterxml.jackson.core" % "jackson-databind" % "2.6.5"

application.conf:

play.application.loader = di.ApplicationLoader

play.filters.csrf.header.bypassHeaders {
  X-Requested-With = "*"
  Csrf-Token = "nocheck"
}
play.filters.csrf.bypassCorsTrustedOrigins = false
play.filters.disabled += play.filters.csrf.CSRFFilter

slick.dbs.default.profile = "slick.jdbc.SQLiteProfile$"
slick.dbs.default.db.driver = "org.sqlite.JDBC"
slick.dbs.default.db.url = "jdbc:sqlite:development.db"
slick.dbs.default.db.username = ""
slick.dbs.default.db.password = ""

db.default {
  driver = org.sqlite.JDBC
  url = "jdbc:sqlite:development.db"
  username = ""
  password = ""
}

play.modules.disabled += "play.api.db.DBModule"

RackRepositorySpec.scala:

import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.guice.GuiceOneAppPerTest
import play.api.db.evolutions._
import play.api.db.slick.DatabaseConfigProvider
import play.api.db.{Database, Databases}
import play.api.inject.bind
import play.api.inject.guice.GuiceInjectorBuilder
import play.api.test.Injecting

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;"
  )
}

我执行sbt test时遇到此错误:

[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.apply(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)

1 个答案:

答案 0 :(得分:0)

我使用build.sbt中的$watch解决了问题。我希望这是光滑的好习惯:

milk_contents <- list.dir(milk folder)
cereal_contents <- list.dir (cereal)
OJ_contents <- list.dir(OJ)
cat_contents <- list.dir(cat)
milk_loc <- "C/Desktop/milk"
cereal_loc <- "C/Desktop/cereal"
OJ_loc <- "C/Desktop/OJ"
cat_loc <- "C/Desktop/cat"