Scala 使用Playframework 2.6 Macwire,通过编译时依赖项注入进行设置和测试

Scala 使用Playframework 2.6 Macwire,通过编译时依赖项注入进行设置和测试,scala,playframework,scalatest,macwire,Scala,Playframework,Scalatest,Macwire,在我的项目中,我有这样的结构 app/ --> common/ --> DefyProductsComponents --> DefyProductsLoader --> controllers/ --> HomeController test/ --> common/ --> DefyProductsServerTest --> controllers --> HomeControl

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

app/
  --> common/
    --> DefyProductsComponents
    --> DefyProductsLoader
  --> controllers/
    --> HomeController
test/
  --> common/
    --> DefyProductsServerTest
  --> controllers
    --> HomeControllerSpec
反产品组件

  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

  }
除副产品加载器

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"))
  }
}
我想设置测试测试,这是测试/结构

app/
  --> common/
    --> DefyProductsComponents
    --> DefyProductsLoader
  --> controllers/
    --> HomeController
test/
  --> common/
    --> DefyProductsServerTest
  --> controllers
    --> HomeControllerSpec
反产品服务器测试

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 {

  }

}
家庭控制器规范

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中创建了一个非常类似的项目。万一有人想玩它,这里最简单的方法是:

在DefyProductsServerTest中,将组件更改为:

然后在测试中,您应该能够做到:

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

答:谢谢您的回答,我尝试了,错误消息已更改错误:22,44找不到类型为:[play.api.mvc.ControllerComponents]的值val controller:HomeController=wire[HomeController]。我会继续努力寻找答案。@agusgambina,你只需要一路拉依赖项,更新我的答案以显示更简单的方法。谢谢你的回答,它确实有效。我现在会详细研究它,因为如果这是最简单的方法,我知道会有更好的方法。有了这个新的解决方案,我不确定还有更好的方法。以前的解决方案创建了2个控制器实例,这就是为什么它不是最好的。