当前位置:  开发笔记 > 编程语言 > 正文

使用编译时DI时,Play 2.4.6中的功能测试

如何解决《使用编译时DI时,Play2.4.6中的功能测试》经验,为你挑选了0个好方法。

我正在使用Play 2.4.6编译时依赖注入和ScalaTest.控制器的构造函数有很少的参数,在ApplicationLoader中我创建它.这是代码:

class BootstrapLoader extends ApplicationLoader {
  def load(context: Context) = {
    new AppComponents(context).application
  }
}

class AppComponents(context: Context) extends BuiltInComponentsFromContext(context) with NingWSComponents {
  lazy val router = new Routes(httpErrorHandler, authenticationController, applicationController, assets)
  lazy val applicationController = new controllers.Application()
  lazy val authenticationController = new controllers.Authentication()(configuration, wsApi.client)
  lazy val assets = new controllers.Assets(httpErrorHandler)
}

class Authentication(implicit configuration: Configuration, val ws: WSClient) extends Controller {
  def login = Action { implicit request =>
    Unauthorized(s"${redirectUrl}")
  }
}

class AuthenticationSpec extends PlaySpec with OneAppPerSuite {

  implicit val configuration: Configuration = app.configuration
  implicit val wsClient: WSClient = WS.client(app)

  "when user not logged-in" should {
    "return Status code Unauthorized(401) with redirect url" in  {
      1 mustEqual 2
    }
  }
}

当我运行测试时,我收到以下错误:

[info] Exception encountered when attempting to run a suite with class name: controllers.AuthenticationSpec *** ABORTED ***
[info]   com.google.inject.ProvisionException: Unable to provision, see the following errors:
[info] 
[info] 1) Could not find a suitable constructor in controllers.Authentication. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
[info]   at controllers.Authentication.class(Authentication.scala:19)
[info]   while locating controllers.Authentication
[info]     for parameter 1 at router.Routes.(Routes.scala:35)
[info]   while locating router.Routes
[info]   while locating play.api.test.FakeRouterProvider
[info]   while locating play.api.routing.Router
[info] 
[info] 1 error
[info]   at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1025)
[info]   at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051)
[info]   at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:321)
[info]   at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:316)
[info]   at play.api.Application$class.routes(Application.scala:112)
[info]   at play.api.test.FakeApplication.routes(Fakes.scala:197)
[info]   at play.api.Play$$anonfun$start$1.apply$mcV$sp(Play.scala:90)
[info]   at play.api.Play$$anonfun$start$1.apply(Play.scala:87)
[info]   at play.api.Play$$anonfun$start$1.apply(Play.scala:87)
[info]   at play.utils.Threads$.withContextClassLoader(Threads.scala:21)

FakeApplication使用GuiceApplicationBuilder,当然不起作用.

我该怎么做才能进行这样的测试?

谢谢

推荐阅读
保佑欣疼你的芯疼
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有