5

My controller method:

def postCategory = Action(parse.tolerantText) { request =>
    Ok("")
  }

and this is my test:

val result = categoryController.postCategory.apply(FakeRequest())
      status(result) mustEqual OK //error this line

I have this error:

Error:(63, 14) type mismatch; found : play.api.libs.streams.Accumulator[akka.util.ByteString,play.api.mvc.Result] required: scala.concurrent.Future[play.api.mvc.Result] status(result) mustEqual OK ^

It seem that using a custom parser parse.* makes it returns Accumulator rather than Future[Result]

I'm using play 2.5-RC2

mmdc
  • 1,677
  • 3
  • 20
  • 32
  • You're right about `Accumulator`. Check this out https://www.playframework.com/documentation/2.5.x/ScalaBodyParsers#Writing-a-custom-body-parser – mfirry Feb 28 '16 at 16:59

2 Answers2

4

You do should use result.run getting instance of Materializer with Guice

would look like:

import akka.stream.Materializer
//...

def mockApp = new GuiceApplicationBuilder().build()
val mtrlzr = mockApp.injector.instanceOf[Materializer]

val result: Accumulator[ByteString, Result] = controller.accessToken()(FakeRequest())
val runResult: Future[Result] = result.run()(mtrlzr)    
Leammas
  • 167
  • 6
2

You can try with something like this:

  val result = categoryController.postCategory.apply(FakeRequest())
  status(result.run) must equalTo(OK)

It basically looks like Accumulator has a nice run() method that returns a Future.

mfirry
  • 3,634
  • 1
  • 26
  • 36
  • the `run()` method requires an `implicit materializer: Materializer` input. It's quite strange to me as didn't have to do this in play 2.3. Do you have any idea? – mmdc Mar 02 '16 at 20:11
  • 1
    My guess is Play 2.5 made the big move towards Akka Streams for anything that has to be done asynchronously. Akka Streams says here [http://doc.akka.io/docs/akka/2.4.2/scala/stream/stream-quickstart.html#stream-quickstart-scala] "The Materializer is a factory for stream execution engines..." – mfirry Mar 03 '16 at 09:38
  • i had the same problem here: https://stackoverflow.com/questions/48036803/play-framework-test-helpers-need-implicit-materializer – tgk Dec 30 '17 at 22:35