Skip to content
Manabu Nakamura edited this page May 11, 2013 · 1 revision

You can use FakeRequest with logged-in status.

package test

import org.specs2.mutable._

import play.api.test._
import play.api.test.Helpers._
import controllers.{AuthConfigImpl, Messages}
import jp.t2v.lab.play2.auth.test.Helpers._

class ApplicationSpec extends Specification {

  object config extends AuthConfigImpl

  "Messages" should {
    "return list when user is authorized" in new WithApplication {
      val res = Messages.list(FakeRequest().withLoggedIn(config)(1))
      contentType(res) must equalTo("text/html")
    }
  }

}
  1. Import jp.t2v.lab.play2.auth.test.Helpers._

  2. Define instance what is mixed-in AuthConfigImpl

     object config extends AuthConfigImpl
    
  3. Call withLoggedIn method on FakeRequest

    • first argument: AuthConfigImpl instance.
    • second argument: user ID of the user who is logged-in at this request

It makes enable to test controllers with play2.auth