I want to test a WS client with a fake server like it's explained in the Play 2.4 documentation here : https://www.playframework.com/documentation/2.4.x/ScalaTestingWebServiceClients
But I'm doing DI with Scaldi and i'm not able to adapt the Play's documentation code to use Scaldi.
Can someone help me ?
The code to adapt is mostly this (come from the Play doc) :
"GitHubClient" should {
"get all repositories" in {
Server.withRouter() {
case GET(p"/repositories") => Action {
Results.Ok(Json.arr(Json.obj("full_name" -> "octocat/Hello-World")))
}
} { implicit port =>
WsTestClient.withClient { client =>
val result = Await.result(
new GitHubClient(client, "").repositories(), 10.seconds)
result must_== Seq("octocat/Hello-World")
}
}
}
}
An example of general approach for integration testing can be found here:
https://github.com/scaldi/scaldi-play-example/blob/master/test/IntegrationSpec.scala#L22
It's not direct equivalent though, since it uses
HttpUnitinstead ofWSClient. More precise equivalent would be something like this:It uses a
WSClient, just like in your example. The difference is thatApplicationandWSClientare injected and test does not rely on a global state or factories.In order to use this example you also need this small helper function which creates a test server based on the injected
Application:Play already provides some helper functions out-of-the-box to create a test server, but most of them either reinitialize an application of rely on Guice. That's because you need to create your own simplified version of it. This function is probably a good candidate for inclusion in scaldi-play.