0

Ultimately my goal is to write a custom Spray Directive for use by various routes in an existing application. I have so far been unable to write a test for my new directive, so I simplified further until my custom code wasn't even in play. I'm still not passing what I believe to be a trivial test. What am I doing wrong?

import org.scalatest.FlatSpec
import spray.http.StatusCodes
import spray.routing.{HttpService, Directives, Route}
import spray.testkit.ScalatestRouteTest

trait TrivialDirectivesTextFixture extends Directives {

  def trivialRoute: Route =
    path("test_route") {
      get { requestContext =>
        println("get")

        complete(StatusCodes.OK, "trivial response")
      }
    }

}

class TrivialDirectivesSpec extends FlatSpec with TrivialDirectivesTextFixture with ScalatestRouteTest with HttpService {

  def actorRefFactory = system

  "TrivialDirectives" should "trivially match" in {
    Get("/test_route") ~> sealRoute(trivialRoute) ~> check {
      println("check")

      assertResult(StatusCodes.OK) {
        println("status " + status)
        status
      }

      assertResult("trivial response") {
        println("response " + response)
        responseAs[String]
      }
    }
  }
}

The resulting output is:

get
check
[info] TrivialDirectivesSpec:
[info] TrivialDirectives
[info] - should trivially match *** FAILED ***
[info]   Request was neither completed nor rejected within 1 second (TrivialDirectivesSpec.scala:30)
broadmonkey
  • 124
  • 2
  • 9

2 Answers2

1

I do not have enough point to write a comment

So I'll write it here, the problem is that

get { 
  complete("OK")
}

is translated to the code below, using some sort of implicit

get { ctx =>
  ctx.complete("OK")
}

thus when you do get { ctx => complete("OK") }

it is not translated properly

Qingwei
  • 510
  • 6
  • 13
0

Solved by changing complete() to requestContext.complete(). I don't really understand why, so I'd appreciate more comprehensive answers.

broadmonkey
  • 124
  • 2
  • 9