Hello!
What I want to do : I try to test a action method addbook
with FakeRequest like below.
val request = FakeRequest()
.withSession("email" -> "admin@admin.co.jp", "name" -> "yun", "id" -> "1")
.withFormUrlEncodedBody(
"name" -> "Great Gatsby",
"price" -> "30",
"author" -> "Scott",
"description" -> "Great classic"
)
.withCSRFToken
val result = homeController.addBook(request).run()(materializer)
flash(result).get("msg") mustBe Some("some msg")
status(result) must equal(SEE_OTHER)
redirectLocation(result) mustBe Some("/somelocation")
What is wrong : But the when I bindFromRequest the Form data, I get nothing but form constraint errors.
[warn] c.HomeController - data :
[warn] c.HomeController - errors : FormError(name,List(error.required),List()), FormError(price,List(error.required),List())
The addbook
action definition is like below
def addBook = isAuthenticatedAsync { (userId, userName, userEmail) =>
implicit request =>
logger.warn("data : " + addBookForm.bindFromRequest.data.mkString(", "))
logger.warn("errors : " + addBookForm.bindFromRequest.errors.mkString(", "))
...
And isAuthenticatedAsync
def isAuthenticatedAsync (f: => (String, String, String) => MessagesRequest[AnyContent] => Future[Result]) = Security.Authenticated(userInfo, onUnauthorized) { user =>
Action.async(request => f(user._1,user._2,user._3)(request))
}
When I change isAuthenticatedAsync
to just Async
method, I can get the form data but I don’t know what I’m missing, why it is not working.
Please tell me what I’m missing?
Have a great day!