播放2反向路由,从控制器方法获取路由

sam*_*sam 6 scala playframework playframework-2.0

使用Play 2.2.3框架,假设我的路由文件中有这样的路由:

GET         /event                                 controllers.Event.events
Run Code Online (Sandbox Code Playgroud)

我怎么能以编程方式获得"/ event"知道我想要达到的方法,在这种情况下'controllers.Authentication.authenticate'?

我需要它用于测试目的,因为我希望有更好的测试等待不是路由本身,而是真正调用的控制器方法.所以也许有另一个解决方案,而不是像我现在这样做路线和测试方式:

//Login with good password and login
    val Some(loginResult) = route(FakeRequest(POST, "/login").withFormUrlEncodedBody(
      ("email", email)
      , ("password", password)))
    status(loginResult)(10.seconds)     mustBe 303
    redirectLocation(loginResult).get mustBe "/event"
Run Code Online (Sandbox Code Playgroud)

Mic*_*jac 25

您可以通过以下方式构建反向路由:

[full package name].routes.[controller].[method]
Run Code Online (Sandbox Code Playgroud)

在你的例子中:

 controllers.routes.Authentication.authenticate
 controllers.routes.Events.events
Run Code Online (Sandbox Code Playgroud)

但是说你打破了你的包都喜欢controllers.authcontrollers.content,那么他们将是:

 controllers.auth.routes.Authentication.authenticate
 controllers.content.routes.Events.events
Run Code Online (Sandbox Code Playgroud)

反向路由返回a Call,其中包含HTTP方法和URI.在您的测试中,您可以构建FakeRequest一个Call:

 FakeRequest(controllers.routes.Authentication.authenticate)
Run Code Online (Sandbox Code Playgroud)

您还可以使用它来测试重定向URI:

 val call: Call = controllers.routes.Events.events 
 redirectLocation(loginResult) must beSome(call.url)
Run Code Online (Sandbox Code Playgroud)