scalasprayspecs2spray-testakka-testkit

Spray route testing with Akka TestProbe


In my Spray route, I delegate to an actor to process the request. The RequestContext is sent in the message to that actor.

path("mypath") {
  parameters("thing".as[String]) { thing => ctx =>
    myActor ! ProcessThingAndResondToContext(thing, ctx)
  }
}

In my test, I substitute a TestProbe for the actor, because the actor's processing is expensive.

class MySpec extends Specification with Specs2RouteTest with ScalaCheck with MyService {
  val testProbe = TestProbe()
  override val myActor = testProbe.ref

  def is = s2"""
    it should $doTheRightThing
  """

  def doTheRightThing = {
    Get(s"/mypath?thing=fruit") ~> route ~> check {
      testProbe.expectMsgClass(classOf[ProcessThingAndResondToContext])
      status mustEqual StatusCodes.Success
    }
  }

This spec fails because there's no status. The TestProbe does nothing and so the ctx was never responded to.

Request was neither completed nor rejected within 1 second

The line status mustEqual StatusCodes.Success is not crucial to my test, but I can't remove it because then the spec doesn't compile - the method no longer typechecks as a MatchResult.

How can I test the route delegated to the actor?


Solution

  • I didn't solve the question, but resolved my problem by using a TestActorRef instead of a TestProbe. Then I could specify a simplified behaviour and still respond to ctx.