Настройка, тестирование с инжекцией зависимостей во время компиляции, с Playframework 2.6 Macwire
В моем проекте у меня есть такая структура
app/
--> common/
--> DefyProductsComponents
--> DefyProductsLoader
--> controllers/
--> HomeController
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
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
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
1 ответ
Решение
Здесь проще всего сделать:
в DefyProductsServerTest
, менять components
в:
override def components: DefyProductsComponents = ...
Тогда в вашем тесте вы сможете сделать:
val home = new components.homeController.index().apply(FakeRequest(GET, "/"))