Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a controller method that uses a custom parser in play 2.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

like image 393
mmdc Avatar asked Feb 28 '16 16:02

mmdc


People also ask

What happens when the controller parses the input from business logic?

Having parsed the input, the controller must transform the input into the model expected by the business logic and pass it on to the business logic. 5. The controller takes the output of the business logic and serializes it into an HTTP response. 6.

How to test if a controller works on a PC?

Test game controller input Connect your game controller to your PC. If you hear the installing hardware chime on Windows 10, allow drivers for it to install. Open your browser and visit Gamepad Tester. Press any button on the controller to wake it up. The controller will be detected by Gamepad ...

Should I use unit testing for the pure controller layer?

However, treat it as an Integration Test since you’re verifying several parts of your application. Don’t skip the Unit Test for the pure Controller layer if you need it. In other words, try to avoid mixing the test layers but rather keep them separate.

Do unit tests need to be done on custom controllers?

Custom Controller Testing Visualforce custom controller is required because custom controllers like all Apex codes, should be covered by unit tests. A custom controller is an Apex class that implements all of the logic for a page without leveraging a standard controller.


2 Answers

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)    
like image 50
Leammas Avatar answered Nov 15 '22 05:11

Leammas


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.

like image 33
mfirry Avatar answered Nov 15 '22 07:11

mfirry