Basic Spray-Testkit usage to test a route does not work
Asked Answered
S

1

2

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
Scrutable answered 16/7, 2014 at 7:50 Comment(4)
can you show code of MyService?Boice
Hello, I edited the description. In the meantime, I found out that the scalatest version I was using was not suitable, so now I am able to run my tests! Which fails because my trait inherits from an actor, but I will fix this. Thanks!Scrutable
Have you seen the spray-template example which for the same reason splits up route definition and HttpService into two things: github.com/spray/spray-template/blob/on_spray-can_1.1/src/main/…Fleeta
The example shows a basic return of html code. My route wants to forward some messages to the application layer, which will be implemented by other actors. So my idea was to retrieve those actors from the ActorSystem or ActorContext. I don't know how to do that from a trait without extending an Actor. I am looking for that right now.Scrutable
S
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.

Scrutable answered 16/7, 2014 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.