Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play modules test & FakeApplication

I would like to know what's the best way to run specs2 tests on a PlayFramework module and be able to simulate it running.

My module contains some routes in a file named mymodule.routes In my apps I integrate them by adding the following line in my routes file

->  /mymodule mymodule.Routes

This is the test from my module I try to run but returns a 404 error :

"test myroute" in {
  running(FakeApplication()) {
    await(WS.url("http://localhost:9000/mymodule/myroute").get).status must equalTo(OK)
  }
}
like image 699
Roch Avatar asked May 16 '13 09:05

Roch


1 Answers

FakeApplication does not really lunch a web process, so you cannot test using http access to localhost.

You have three options:

  • Testing the controller directly
  • Testing the router
  • Testing the whole app.

Testing the controller is done by directly calling your controller and checking the result, as suggested in play documentation, and providing a FakeRequest()

val result = controllers.Application.index("Bob")(FakeRequest())

Testing the router is done by calling routeAndCall with a FakeRequest argument, specifying the relative path:

val Some(result) = routeAndCall(FakeRequest(GET, "/Bob"))

Eventually, if you want to test your whole application, you need to start a TestServer:

"run in a server" in {
  running(TestServer(3333)) {

    await(WS.url("http://localhost:3333").get).status must equalTo(OK)

  }
}

Your question says: "What is the best option?". The answer is: there is nothing such as a best option, there are different way of testing for different purpose. You should pick up the testing strategy which better fits your requirement. In this case, since you want to test the router, I suggest you try approach n.2

like image 71
Edmondo1984 Avatar answered Sep 18 '22 08:09

Edmondo1984