title: Play Framework, Specs2 - Calling controller method directly from unit test
body: <p>I have a method in my controller that I'd like to call directly. It takes in a POSTed form, validates it, then returns something. I want to test this directly - i.e. not go through the routes helper. </p>
  
  <p>This is my Form code (FormFields is just a case class)</p>
  
  <pre><code>val searchForm = Form(
    mapping(
     "foo" -&gt; nonEmptyText,
     "filter" -&gt; optional(text).verifying("Filter text must be 'published' or 'unpublished'",
       x =&gt; x.isEmpty || x.get == "published" || x.get == "unpublished")
   )(FormFields.apply)(FormFields.unapply)
  </code></pre>
  
  <p>)</p>
  
  <p>This is my controller call. </p>
  
  <pre><code>def doThings() = IsAuthenticated {
     username =&gt; implicit request =&gt; {
      searchForm.bindFromRequest().fold(
        formWithErrors =&gt; BadRequest(s"Incorrect data: ${formWithErrors.errors.map(x =&gt; s"${x.key} ${x.message}").mkString}."),
        form =&gt; {
              OK("Test text here")
        }
      )
    }
  </code></pre>
  
  <p>}</p>
  
  <p>If I call this via my routes file, as below - This works as expected. Form gets posted, verified, returns OK("Test...") as expected.</p>
  
  <p>ie. The below works (using Specs2)</p>
  
  <pre><code>        val request = FakeRequest(POST, "notarealurl")
            .withFormUrlEncodedBody(
            "filter" -&gt; "published",
            "foo" -&gt; "Test"
      ).withSession("email" -&gt; "testuser")
  
      val Some(result) = route(request)
      status(result) must equalTo(OK)
  </code></pre>
  
  <p>However, whatever I try to call the method directly fails - The failure happens on the form validation step. It tells me that "foo" is missing a value when I run the unit test. This is how I'm attempting to do this.</p>
  
  <pre><code>    val request = FakeRequest()
        .withFormUrlEncodedBody(
        "filter" -&gt; "published",
        "foo" -&gt; "Test"
      ).withSession("email" -&gt; "testuser")
  
  
      //val Some(result) = route(request)
      val result = Search.searchProducts()(request)
  
      println(contentAsString(result))
      status(result) must equalTo(OK)
  </code></pre>
  
  <p>The text printed is "Incorrect search: foo error.required." I think I'm not doing the call properly, but I don't know where I'm going wrong.</p>
  
  <p>Note : The code here represents my problem but has been cut down to just illustrate the issue.</p>