I'm using Guice to inject components inside an actor as it is explained in the Play! Scala 2.5 documentation.
In my application, I inject unshortLinksFactory: UnshortLinks.Factory
in my classes and I create a new actor like this:
val unshortLinksActor = actorSystem.actorOf(Props(unshortLinksFactory(ws)))
The problem is that I cannot inject components in my test class (can I?) otherwise the test are not started. (Please note that I use Scalatest.)
How can I create the actor in my tests? It's fine if I can create it like:
val unshortLinksActor = system.actorOf(Props(unshortLinksFactory(ws)))
but the best would be to be able to create it with TestActorRef
from Akka.testKit
in order to have access to the underlyingActor
.
What I do in order to test it is:
I extends the test class with TestKit(ActorSystem("testSystem"))
.
Then I create the Props
like this:
lazy val unshortLinkFactoryProps = Props(unshortLinkFactory(
dbConfigProvider = dbConfProvider)
Here dbConfProvider
is created like this but could also be mocked:
lazy val appBuilder = new GuiceApplicationBuilder()
lazy val injector = appBuilder.injector()
lazy val dbConfProvider = injector.instanceOf[DatabaseConfigProvider]
Finally I can have an actorRef like this:
val actorRef = TestActorRef[UnshortLinksActor](unshortLinksFactoryProps)
And I can access the methods inside my actor with actorRef.underlyingActor
.