question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit testing EssentialAction

See original GitHub issue

When we are trying to test an essential action the status is always 500

val a = EssentialAction { request =>
 Action { Ok("") }(request)
}
Helpers.await(a(FakeRequest()).run).header.status must be equalTo OK

Is it a bug?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:17 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
yannscommented, Nov 12, 2013

Sorry, I do not want to be rude, but I am not happy with the answer.

The question was about unit testing, and starting a FakeApplication is not an optimal way to unit test. FakeApplication is intended to be used in functional tests: http://www.playframework.com/documentation/2.2.x/ScalaTest

On my opinion, starting a FakeApplication for an unit test is not a viable solution because:

  • it makes the test much slower
  • the test cannot be run in parallel anymore

And I do not really understand why a such difference between Action and EssentialAction.

For example, it is very easy to unit test an Action, even if it consumes a body:

    val action = Action { request =>
      val value = (request.body.asJson.get \ "field").as[String]
      Ok(value)
    }

    val request = FakeRequest(POST, "/")
      .withJsonBody(Json.parse("""{ "field": "value" }"""))
    val result = action.apply(request)
    assert(status(result) === OK)
    assert(contentAsString(result) === "value")

(scala test)

By simply changing the type of the Action, it is much more complicated:

    val action: EssentialAction = Action { request =>
      val value = (request.body.asJson.get \ "field").as[String]
      Ok(value)
    }

    val request = FakeRequest(POST, "/")
      .withHeaders(CONTENT_TYPE -> "application/json")

    val requestBody = Enumerator("""{ "field": "value" }""".getBytes) andThen Enumerator.eof
    Play.start(FakeApplication())
    try {
      val result: Future[SimpleResult] = requestBody |>>> action(request)

      assert(status(result) === OK)
      assert(contentAsString(result) === "value")
    } finally {
      Play.stop()
    }

On my opinion:

  • it should be possible to unit test an EssentialAction without any started FakeApplication
  • there should be an API to easily unit test an EssentialAction.
  • it should be documented how to unit test an EssentialAction.

That would help this framework to be even more awesome for testing.

0reactions
yannscommented, Nov 25, 2013
Read more comments on GitHub >

github_iconTop Results From Across the Web

Play 2.1: unit testing EssentialActions - Stack Overflow
An essential action is a RequestHeader => Iteratee[Indata, Result] , you can apply it to FakeRequest since it implements RequestHeader .
Read more >
Testing your application with ScalaTest - Play Framework
Unit Testing EssentialAction. Testing Action or Filter can require testing an EssentialAction (more information about what an EssentialAction is). For this, ...
Read more >
Play 2.5 Unit testing controller failed with ScalaTestPlus
In my controller unit test, I got something like above before. However when I upgrade to Play 2.5, I got some error: could...
Read more >
enlarge your test scope - Blog
A traditional approach of unit test is to consider one class or function of one ... String), EssentialAction] , making really easy to...
Read more >
Action Officer Skills For Your Resume And Career - Zippia
... coding comments, unit testing, integration testing, design documents, ... 15 Essential Action Officer Skills For Your Resume And Career ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found