首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PlayFrame2.6Macwire建立编译时依赖注入测试

使用PlayFrame2.6Macwire建立编译时依赖注入测试
EN

Stack Overflow用户
提问于 2018-03-21 15:33:23
回答 1查看 403关注 0票数 0

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

代码语言:javascript
复制
app/
  --> common/
    --> DefyProductsComponents
    --> DefyProductsLoader
  --> controllers/
    --> HomeController

DefyProductsComponents

代码语言:javascript
复制
  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

代码语言:javascript
复制
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

代码语言:javascript
复制
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/结构

代码语言:javascript
复制
test/
  --> common/
    --> DefyProductsServerTest
  --> controllers
    --> HomeControllerSpec

DefyProductsServerTest

代码语言:javascript
复制
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

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-21 16:02:31

这里最简单的方法是:

DefyProductsServerTest中,将components更改为:

代码语言:javascript
复制
override def components: DefyProductsComponents = ...

那么,在您的测试中,您应该能够:

代码语言:javascript
复制
val home = new components.homeController.index().apply(FakeRequest(GET, "/"))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49410485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档