I have a simple demonstration of this problem, having reduced the code to the smallest thing that I can get to fail. My controller looks like this
package controllers
import play.api.mvc._
import play.api.mvc.BodyParsers.parse._
object WithProblem extends Controller {
def index: Action[String] = Action(parse.text) {
implicit request: Request[_] => Ok("Hello World")
}
}
object WithOutProblem extends Controller {
def index: Action[AnyContent] = Action {
implicit request: Request[_] => Ok("Hello World")
}
}
My test class is
import org.scalatestplus.play.PlaySpec
import play.api.mvc._
import play.api.test.FakeRequest
import play.api.test.Helpers._
class NotaryControllerSpec extends PlaySpec with Results {
"The WithProblem Controller Spec" should {
"compile" in {
val request = FakeRequest("GET", "/")
val result = WithProblem.index()(request)
contentAsString(result) mustBe ("Hello World")
}
}
"The WithOutProblem Controller Spec" should {
"compile" in {
val request = FakeRequest("GET", "/")
val result = WithOutProblem.index()(request)
contentAsString(result) mustBe ("Hello World")
}
}
I have an error message
overloaded method value contentAsString with alternatives:
(of: scala.concurrent.Future[play.api.mvc.Result])(implicit timeout: akka.util.Timeout)String <and>
(of: play.twirl.api.Content)(implicit timeout: akka.util.Timeout)String
cannot be applied to (play.api.libs.iteratee.Iteratee[Array[Byte],play.api.mvc.Result]) WithProblemSpec.scala /Iteratees/test/controllers
The line it references is the 'WithProblem' usage of contentAsString(result) mustBe ("Hello World")
The software stack I am using is as follows. I checked today on http://ift.tt/1mFxyml and this looks like the correct version of Scalatestplus play
- Windows 8
- Java 1.8.0_40-b26
- Scala 2.11.7
- Play 2.4.4
- ScalaPlusPlayText "org.scalatestplus" %% "play" % "1.4.0-M4"
I realise by the way that the test won't work. The Action 'WithProblem.index' actually returns a request code 415, as there isn't any body text sent to it by the text. My question though is about why I have an Iteratee[Array[Byte], Result] instead of a Future[Result], and how I should work with it. i.e. how do I write tests for Actions with BodyParsers
My actual application is for File uploading: I want to calculate the digest of a file, without having to save the file to the hard drive. I think at the moment that to do this idiomatically in play, I should make a body parser that uses an Iteratee and produces a string that is the digest of that file.
Aucun commentaire:
Enregistrer un commentaire