[英]Creating functional tests Scala Playframework 2.6 Macwire
I wrote some traits to use it as a base for my functional tests 我写了一些特征以用作功能测试的基础
This file is for creating a DB in memory (H2 + Evolutions) 该文件用于在内存中创建数据库(H2 + Evolutions)
BlogApiDBTest.scala BlogApiDBTest.scala
package functional.common
import play.api.db.Databases
import play.api.db.evolutions.Evolutions
trait BlogApiDBTest {
implicit val testDatabase = Databases.inMemory(
name = "blog_db",
urlOptions = Map(
"MODE" -> "MYSQL"
),
config = Map(
"logStatements" -> true
)
)
org.h2.engine.Mode.getInstance("MYSQL").convertInsertNullToZero = false
Evolutions.applyEvolutions(testDatabase)
}
Here I am overriding some injected components for testing purposes 在这里,我为了测试目的覆盖了一些注入的组件
BlogApiComponentsTest.scala BlogApiComponentsTest.scala
package functional.common
import common.BlogApiComponents
import org.scalatestplus.play.components.WithApplicationComponents
import play.api.{BuiltInComponents, Configuration}
trait BlogApiComponentsTest extends WithApplicationComponents with BlogApiDBTest {
override def components: BuiltInComponents = new BlogApiComponents(context) {
override lazy val configuration: Configuration = context.initialConfiguration
override lazy val blogDatabase = testDatabase
}
}
This is the base class for my functional tests 这是我的功能测试的基类
BlogApiOneServerPerTestWithComponents.scala BlogApiOneServerPerTestWithComponents.scala
package functional.common
import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.components.{OneServerPerTestWithComponents}
trait BlogApiOneServerPerTestWithComponents extends PlaySpec with OneServerPerTestWithComponents with BlogApiComponentsTest {
}
Finally the test I am trying to execute 最后,我要执行的测试
PostControllerSpec.scala PostControllerSpec.scala
package functional.controllers
import functional.common.BlogApiOneServerPerTestWithComponents
import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures}
import play.api.mvc.{Results}
import play.api.test.{FakeRequest, Helpers}
import play.api.test.Helpers.{GET, route}
class PostControllerSpec extends BlogApiOneServerPerTestWithComponents
with Results
with ScalaFutures
with IntegrationPatience {
"Server query should" should {
"provide an Application" in {
val Some(result) = route(app, FakeRequest(GET, "/posts"))
Helpers.contentAsString(result) must be("success!")
}
}
}
Then I get 然后我得到
blog-api/test/functional/controllers/PostControllerSpec.scala:18:31: Cannot write an instance of play.api.mvc.AnyContentAsEmpty.type to HTTP response. blog-api / test / functional / controllers / PostControllerSpec.scala:18:31:无法将play.api.mvc.AnyContentAsEmpty.type的实例写入HTTP响应。 Try to define a Writeable[play.api.mvc.AnyContentAsEmpty.type] 尝试定义一个Writeable [play.api.mvc.AnyContentAsEmpty.type]
Adding the following import should make it work: 添加以下导入将使其起作用:
import play.api.test.Helpers._
Looking at the signature of route
看route
的签名
def route[T](app: Application, req: Request[T])(implicit w: Writeable[T]): Option[Future[Result]]
we see it expects an implicit w: Writeable[T]
. 我们看到它期望一个implicit w: Writeable[T]
。 The above import will provide it via Writables 上面的导入将通过Writables提供
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.