Basic Spray-Testkit usage to test a route does not

2019-03-01 07:06发布

问题:

I am trying to use spray route and want to test it with Spray-TestKit. I am using : - Scala 2.10.3 - Akka 2.3.3 - Spray 1.3.1

I create a trait extending HttpService, where I define a route :

trait MyService extends HttpService with CoreAccess {
  import greentee.am.endpoint.tsmsp.tsmSPJsonSupport._


  val myRoute = {
    path("resources"/"ping") {
      get {
        complete(OK, "pong")
      }
    } 
  }
}

I deleted part of the route which was not relevant. CoreAccess is a trait extending Actor, because I have methods in that trait access the ActorSystem. (I don't know who to retrieve ActorSelection from a trait without it extending an actor)

Then I create a test Specification

import MyService
import org.specs2.mutable.Specification
import spray.testkit.Specs2RouteTest
import spray.http.StatusCodes._


class RegistrationRouteSpecification extends Specification with Specs2RouteTest with MyService {

  def actorRefFactory = system

  "The EndPoint " should {
    "return Pong to a Get request to the ping" in {
      Get("/resources/ping") ~> myRoute ~> check {
        status === OK
        responseAs[String] === "pong"
      }
    }
  }
}

When I try to execute the test, I get the following compilation error:

[info] Compiling 1 Scala source to /Users/IdeaProjects/endpoint/target/scala-2.10/test-classes...
[error] /Users/IdeaProjects/endpoint/src/test/scala/RegistrationRouteSpecification.scala:19: could not find implicit value for parameter ta: RegistrationRouteSpecification.this.TildeArrow[spray.routing.RequestContext,Unit]
[error]       Get("/resources/ping") ~> myRoute ~> check {
[error]                                     ^
[error] one error found

回答1:

I answer my own question. I corrected my Build.scala to use the following lines:

val scalaCheck   = "org.scalacheck"             %% "scalacheck"     % Versions.scalaCheckVersion % "test"
val scalaTest =  "org.scalatest"   %%   "scalatest"  % "2.2.0" % "test"

Instead of using a simple '%' and supplying a dedicated version.