Game 2.1: Unit Testing EssentialActions

I want a unit test controller method that returns an EssentialAction . I pass FakeRequest him and return to Iteratee[Array[Byte], Result] .

It seems that the test assistants contentAsString , contentType and status do not accept this type of result.

Is there an implicit conversion that I am missing? Is there an example somewhere in the controllers that are tested per unit without calling whole FakeApplication ?

+4
source share
2 answers

The essential action is RequestHeader => Iteratee[Indata, Result] , you can apply it to FakeRequest since it implements RequestHeader . To actually perform an iteration, you either populate it with data, or simply immediately indicate that there are no more indates. For both cases, you get Future[Result] back, which you need to wait in tests.

So, for a simple GET without the request body (using the wait method of the playback helper), you can do this as follows:

 val iteratee = controllers.SomeController.action()(FakeRequest()) val result: Result = await(iteratee.run) 

If you want to make requests with the request authorities, you will need to do something else in order to be able to submit the body of the request for iteration, as well as correctly process the encoding data of your indata.

+3
source

In Play 2.3, PlaySpecification includes several helper methods. To handle EssentialActions, you must use call . As a result, the future will be handled by other more specific assistants.

 class MySpec extends PlaySpecification { ... val result1: Result = call(controllers.SomeController.action(), FakeRequest(...)) status(of = result1) must equalTo (OK) ... val result2 = call(controllers.SomeController.action(), RequestHeader(...), "Body") status(of = result2) must equalTo (BAD_REQUEST) } 
+1
source

Source: https://habr.com/ru/post/1492850/


All Articles