Scala 如何测试Play 2.0中的Zentasks示例应用程序
我使用Play2.0,Scala版本。目前,我分析 此应用程序的一部分是身份验证机制,主要包含在Scala 如何测试Play 2.0中的Zentasks示例应用程序,scala,testing,playframework-2.0,Scala,Testing,Playframework 2.0,我使用Play2.0,Scala版本。目前,我分析 此应用程序的一部分是身份验证机制,主要包含在Securedtrait中。我想知道如何从中测试安全操作,例如索引 对于不安全的操作,我可能会执行以下操作 val result = controllers.Projects.index(FakeRequest()) 运行操作并获得其结果 在担保诉讼的情况下,我应该怎么做 免责声明:我对Scala和Play都是全新的,所以所有提示都非常有价值。谢谢 好吧,我也不是什么伟大的专家,但这里有一个想法 创
Secured
trait中。我想知道如何从中测试安全操作,例如索引
对于不安全的操作,我可能会执行以下操作
val result = controllers.Projects.index(FakeRequest())
运行操作并获得其结果
在担保诉讼的情况下,我应该怎么做
免责声明:我对Scala和Play都是全新的,所以所有提示都非常有价值。谢谢 好吧,我也不是什么伟大的专家,但这里有一个想法
创建一个trait unsecure trait extensed Secured
,它覆盖安全操作并始终允许访问。
然后,您可以在测试中创建一个对象unsecureprojects extends Projects with unsecure
,这应该只覆盖安全检查,并允许您在没有任何安全性的情况下测试操作
现在,您不再在项目上运行测试,而是在不安全的项目上运行测试。您可以对其他安全控制器执行完全相同的操作
我还没有测试过,所以请告诉我它是否有效;) Playframewrk v2.1中有一个我有一个
在它被合并和发布之前,以下是我所做的(它在Play 2.0.3+上工作):
我在libs包中定义了自己的Helpers对象,如下所示
package libs
import play.api.mvc._
import play.api.libs.iteratee._
import play.api.libs.concurrent._
import play.api.test._
object Helpers {
def routeAndCall[T](request: FakeRequest[T]): Option[Result] = {
routeAndCall(this.getClass.getClassLoader.loadClass("Routes").asInstanceOf[Class[play.core.Router.Routes]], request)
}
/**
* Use the Router to determine the Action to call for this request and executes it.
*/
def routeAndCall[T, ROUTER <: play.core.Router.Routes](router: Class[ROUTER], request: FakeRequest[T]): Option[play.api.mvc.Result] = {
val routes = router.getClassLoader.loadClass(router.getName + "$").getDeclaredField("MODULE$").get(null).asInstanceOf[play.core.Router.Routes]
routes.routes.lift(request).map {
case a: Action[_] =>
val action = a.asInstanceOf[Action[T]]
val parsedBody: Option[Either[play.api.mvc.Result, T]] = action.parser(request).fold(
(a, in) => Promise.pure(Some(a)),
k => Promise.pure(None),
(msg, in) => Promise.pure(None)
).await.get
parsedBody.map{resultOrT =>
resultOrT.right.toOption.map{innerBody =>
action(FakeRequest(request.method, request.uri, request.headers, innerBody))
}.getOrElse(resultOrT.left.get)
}.getOrElse(action(request))
}
}
}
然后我使用Around设置我的应用程序(我需要提供application.secret,因为我在基于签名cookie的会话中存储经过身份验证的用户名)
它允许您执行安全代码,即使它不是单元测试
import libs.Helpers._
import play.api.test.Helpers.{routeAndCall => _,_}
def appWithSecret():Map[String,String]={
Map(("application.secret","the answer is 42 !"))
}
object emptyApp extends Around {
def around[T <% Result](t: => T) = {
running(FakeApplication(additionalConfiguration = inMemoryMongoDatabase("emptyApp")++appWithSecret())) {
User(new ObjectId, "Jane Doe", "foobar@example.com", "id1").save()
t // execute t inside a http session
}
}
}
"respond to the index Action" in emptyApp {
val request: FakeRequest[AnyContent] = FakeRequest(GET, "/expenses").withSession(("email", "foobar@example.com"))
val Some(result) = routeAndCall(request)
status(result) must equalTo(OK)
contentType(result) must beSome("application/json")
charset(result) must beSome("utf-8")
contentAsString(result) must contain("Hello Bob")
}