简体   繁体   English

使用Playframework 2.6 Macwire进行设置,使用编译时依赖性注入进行测试

[英]Setting up, Testing with compile-time Dependency Injection, with Playframework 2.6 Macwire

In my project I have this structure 在我的项目中,我有这个结构

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

DefyProductsComponents DefyProductsComponents

  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 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
  }

}

HomeController HomeController的

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

I want to setup the test the tests, this is the test/ structure 我想设置测试测试,这是test/结构

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

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

This setup I wrote is not working and I am not sure why in the HomeControllerSpec homeController is not found. 我写的这个设置不起作用,我不确定为什么在HomeControllerSpec找不到homeController。 I understand that in the tests all components from DefyProductsComponents are available and I am able to override 我了解在测试中, DefyProductsComponents中的所有组件均可用,并且我可以覆盖


UPDATE: I created a very similar project in github. 更新:我在github中创建了一个非常相似的项目。 Just in case someone want to play with it https://github.com/agusgambina/play-scala-base.git 万一有人想玩它https://github.com/agusgambina/play-scala-base.git

The easiest to do here is: 最简单的方法是:

in DefyProductsServerTest , change components into: DefyProductsServerTest ,将components更改为:

override def components: DefyProductsComponents = ...

Then in your test you should be able to do: 然后,在测试中,您应该能够:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM