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)
}
}
FakeApplication does not really lunch a web process, so you cannot test using http access to localhost.
You have three options:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With