简体   繁体   English

创建功能测试Scala Playframework 2.6 Macwire

[英]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]

Here is the code 这是代码

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.

相关问题 Playframework 2.6 [Scala]:如何在进行任何实际的基本测试之前运行设置测试? - Playframework 2.6 [Scala]: How to run my setup tests before any actual essential tests? 使用Playframework 2.6 Macwire进行设置,使用编译时依赖性注入进行测试 - Setting up, Testing with compile-time Dependency Injection, with Playframework 2.6 Macwire 在scala playframework(2.6)slick中向Gmail发送邮件 - Sending a Mail to Gmail in scala playframework(2.6) slick 带有 ReactiveMongo 的 Scala 中的多租户 Playframework 2.6 应用程序 - Multitenant Playframework 2.6 application in Scala with ReactiveMongo Scala,Cake Pattern和MacWire - Scala, Cake Pattern, and MacWire playframework scala在测试中验证案例类 - playframework scala validate case class in tests playframework 2.0 scala - 在测试中找不到合适的驱动程序 - playframework 2.0 scala - No suitable driver found in tests 为Scala和Playframework中的嵌套类创建QueryStringBindable - Creating a QueryStringBindable for a nested classes in Scala and Playframework PlayFramework 2.6关机挂钩 - PlayFramework 2.6 shutdown hook 使用 scala 进行功能测试的更好方法是什么? - What is the better approach on functional testing playframework with scala with Injection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM