【发布时间】:2013-07-18 23:21:47
【问题描述】:
非常感谢您的指导!
当我从浏览器运行 SecureSocial 插件时,它运行良好,但我希望现在能够测试我的 Play 应用程序的其余部分。
快速介绍
SecureSocial 的语法如下所示:
def page = SecuredAction(WithProvider("google")) { ... }
或者这个:
def page = UserAwareAction { ... }
我一直在寻找here,这似乎是 Stack Overflow 上唯一的问题,甚至与我的 SecureSocial 问题远程相关,但我不太喜欢重新连接字节码。应该有一个更简单的解决方案。
当我运行访问 SecureSocial 保护的操作的测试时,我收到一个大错误,我猜这基本上意味着我没有将它传递给用户。 (见本题底部)
我想做的事
仅在测试期间注入所有函数以返回类型 Action 而不是 SecuredAction 或 UserAwareAction
或者实际上将测试用户传递给调用。 但是怎么做呢?
我有什么
@Singleton
class JsonOps @Inject() () extends Controller with SecureSocial {...}
按照here 的描述编写的Global.scala 并在我的测试中...
val controller = new JsonOps
val result = controller.userAwareActionRequestForSomeJson("")(FakeRequest())
我也有这样的电话:
// This is what I would use for production
def extjs = SecuredAction(WithProvider("google")) { implicit request =>
Ok(views.html.extjs(request.user.firstName))
}
// This is what I would use for testing
def extjs = Action { implicit request =>
Ok(views.html.extjs("testtesttesting"))
}
这就是为什么我认为这个问题可能非常适合依赖注入?不过,我不确定如何进行类实例化,因为我使用的 Global.scala 是一个通用类实例化器。我也不特别希望为我拥有的每个控制器编写 9000 多个特征。
大错误
这是行 UserOpsSpec.scala 第 12 和 13 行:
12 val controller = new UserOps
13 val result = controller.extjs()(FakeRequest())
这是错误
[error] RuntimeException: java.lang.ExceptionInInitializerError (UserOpsSpec.scala:13)
[error] play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:220)
[error] securesocial.core.SecureSocial$.authenticatorFromRequest(SecureSocial.scala:200)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:81)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:78)
[error] play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:215)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:51)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:51)
[error] play.api.Play$.current(Play.scala:51)
[error] securesocial.core.Authenticator$.cookieName$lzycompute(Authenticator.scala:188)
[error] securesocial.core.Authenticator$.cookieName(Authenticator.scala:188)
[error] securesocial.core.Authenticator$.<init>(Authenticator.scala:201)
[error] securesocial.core.Authenticator$.<clinit>(Authenticator.scala)
[error] securesocial.core.SecureSocial$.authenticatorFromRequest(SecureSocial.scala:200)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:81)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:78)
[error] play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:215)
[error] null
[error] securesocial.core.SecureSocial$.authenticatorFromRequest(SecureSocial.scala:200)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:81)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:78)
[error] play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:215)
[error] There is no started application
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:51)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:51)
[error] play.api.Play$.current(Play.scala:51)
[error] securesocial.core.Authenticator$.cookieName$lzycompute(Authenticator.scala:188)
[error] securesocial.core.Authenticator$.cookieName(Authenticator.scala:188)
[error] securesocial.core.Authenticator$.<init>(Authenticator.scala:201)
[error] securesocial.core.Authenticator$.<clinit>(Authenticator.scala)
[error] securesocial.core.SecureSocial$.authenticatorFromRequest(SecureSocial.scala:200)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:81)
[error] securesocial.core.SecureSocial$$anonfun$SecuredAction$1.apply(SecureSocial.scala:78)
[error] play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:215)
[info]
【问题讨论】:
标签: scala testing guice playframework-2.1 securesocial