【问题标题】:Setting up, Testing with compile-time Dependency Injection, with Playframework 2.6 Macwire使用 Playframework 2.6 Macwire 设置,使用编译时依赖注入进行测试
【发布时间】:2018-08-30 19:38:01
【问题描述】:

在我的项目中,我有这个结构

app/
  --> common/
    --> DefyProductsComponents
    --> DefyProductsLoader
  --> controllers/
    --> HomeController

DefyProducts组件

  package common

  import com.softwaremill.macwire.wire
  import controllers.{Assets, AssetsComponents, HomeController}
  import play.api.ApplicationLoader.Context
  import play.api.BuiltInComponentsFromContext
  import play.api.routing.Router
  import play.filters.HttpFiltersComponents
  import router.Routes

  import scala.concurrent.Future

  class DefyProductsComponents(context: Context)
    extends BuiltInComponentsFromContext(context)
      with HttpFiltersComponents
      with AssetsComponents {

    // Controllers
    lazy val homeController = wire[HomeController]

    // Router
    override lazy val assets = wire[Assets]
    lazy val prefix: String = "/"
    lazy val defyProductsRouter: Router = wire[Routes]
    lazy val router: Router = defyProductsRouter

  }

DefyProductsLoader

package common

import play.api._
import play.api.ApplicationLoader.Context

class DefyProductsLoader extends ApplicationLoader {

  override def load(context: Context): Application = {
    LoggerConfigurator(context.environment.classLoader).foreach { _.configure(context.environment) }
    new DefyProductsComponents(context).application
  }

}

家庭控制器

package controllers

import play.api.mvc._

class HomeController (val controllerComponents: ControllerComponents) extends BaseController {

  def index() = Action { implicit request: Request[AnyContent] =>
    Ok(views.html.index("Welcome to Play"))
  }
}

我想设置测试测试,这是test/ 结构

test/
  --> common/
    --> DefyProductsServerTest
  --> controllers
    --> HomeControllerSpec

DefyProductsServerTest

package common

import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.components.OneAppPerTestWithComponents
import play.api.{BuiltInComponents, NoHttpFiltersComponents}

class DefyProductsServerTest extends PlaySpec with OneAppPerTestWithComponents {

  override def components: BuiltInComponents = new DefyProductsComponents(context) with NoHttpFiltersComponents {

  }

}

HomeControllerSpec

package controllers

import common.DefyProductsServerTest
import play.api.test._
import play.api.test.Helpers._

class HomeControllerSpec extends DefyProductsServerTest {

  "HomeController GET" should {

    "render the index page from the application" in {
      val home = homeController.index().apply(FakeRequest(GET, "/"))

      status(home) mustBe OK
      contentType(home) mustBe Some("text/html")
      contentAsString(home) must include ("Welcome to Play")
    }

    "render the index page from the router" in {
      val request = FakeRequest(GET, "/")
      val home = route(app, request).get

      status(home) mustBe OK
      contentType(home) mustBe Some("text/html")
      contentAsString(home) must include ("Welcome to Play")
    }
  }
}

我写的这个设置不起作用,我不确定为什么在HomeControllerSpec homeController 中找不到。我知道在测试中来自DefyProductsComponents 的所有组件都可用,我可以覆盖


更新:我在 github 中创建了一个非常相似的项目。以防万一有人想玩它https://github.com/agusgambina/play-scala-base.git

【问题讨论】:

    标签: scala playframework scalatest macwire


    【解决方案1】:

    这里最简单的做法是:

    在DefyProductsServerTest 中,将components 更改为:

    override def components: DefyProductsComponents = ...
    

    那么在你的测试中你应该能够做到:

    val home = new components.homeController.index().apply(FakeRequest(GET, "/"))
    

    【讨论】:

    • A.谢谢你的回答,我试过了,错误信息改变了Error:(22, 44) Cannot find a value of type: [play.api.mvc.ControllerComponents] val controller: HomeController = wire[HomeController]。我会继续努力寻找答案。
    • @agusgambina,您只需要一路拉取依赖项,更新我的答案以显示更简单的方式
    • A.谢谢您的回答,它确实有效。我现在详细看一下,因为如果这是最简单的方法,我明白会有更好的方法。
    • 有了这个新的解决方案,我不确定还有没有更好的方法。之前的解决方案创建了 2 个控制器实例,这就是为什么它不是最好的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多