Skip to content

Instantly share code, notes, and snippets.

@justjoheinz
Created November 15, 2013 08:32
Show Gist options
  • Save justjoheinz/7481030 to your computer and use it in GitHub Desktop.
Save justjoheinz/7481030 to your computer and use it in GitHub Desktop.
Gist to describe problems testing actors which are instantiated in a Play2 controller.
package controllers
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits._
import play.api.Play.current
import play.api.libs.concurrent.Akka
import play.api.mvc._
import akka.actor._
import akka.actor.actorRef2Scala
import akka.pattern.ask
import akka.util._
import akka.util.Timeout.durationToTimeout
object Application extends Controller {
val justAnActor = Akka.system.actorOf(Props[JustAnActor], "justAnActor")
implicit val timeout: Timeout = 5 seconds
def index = Action.async {
(justAnActor ? "Akka").mapTo[String]
.map(answer => Ok(answer))
}
}
class JustAnActor extends Actor {
def receive = {
case _: String => sender ! s"Hello Akka"
}
}
import org.junit.runner.RunWith
import play.api.test._
import org.specs2.runner.JUnitRunner
import scala.concurrent.Await
import scala.concurrent.duration._
import org.specs2.mutable.Specification
@RunWith(classOf[JUnitRunner])
class ApplicationSpec extends PlaySpecification {
sequential
"FakeApplication" should {
"send OK 1" in new WithApplication{
//route(FakeRequest(GET, "/")).get
val home = route(FakeRequest(GET, "/")).get
status(home) must equalTo(OK)
}
"send OK 2" in new WithApplication{
val home = route(FakeRequest(GET, "/")).get
status(home) must equalTo(OK)
}
}
"Controller" should {
"send OK 1" in new WithApplication {
val result = Await.result(controllers.Application.index(FakeRequest("GET","/")) , 5 seconds)
result.header.status must beEqualTo(200)
}
"send OK 2" in new WithApplication {
val result = Await.result(controllers.Application.index(FakeRequest("GET","/")) , 5 seconds)
result.header.status must beEqualTo(200)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment