【发布时间】:2014-09-06 15:30:48
【问题描述】:
我正在尝试使用喷涂路线并希望使用 Spray-TestKit 对其进行测试。 我在用 : - 斯卡拉 2.10.3 - 阿卡 2.3.3 - 喷雾 1.3.1
我创建了一个扩展 HttpService 的特征,我在其中定义了一个路由:
trait MyService extends HttpService with CoreAccess {
import greentee.am.endpoint.tsmsp.tsmSPJsonSupport._
val myRoute = {
path("resources"/"ping") {
get {
complete(OK, "pong")
}
}
}
}
我删除了部分不相关的路线。 CoreAccess 是一个扩展 Actor 的 trait,因为我在该 trait 中有访问 ActorSystem 的方法。 (如果不扩展actor,我不知道谁可以从特征中检索ActorSelection)
然后我创建一个测试规范
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"
}
}
}
}
当我尝试执行测试时,出现以下编译错误:
[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
【问题讨论】:
-
你能显示
MyService的代码吗? -
您好,我编辑了描述。同时,我发现我使用的 scalatest 版本不合适,所以现在我可以运行我的测试了!失败是因为我的特质继承自演员,但我会解决这个问题。谢谢!
-
你见过spray-template例子吗,出于同样的原因,它把路由定义和HttpService分成两部分:github.com/spray/spray-template/blob/on_spray-can_1.1/src/main/…
-
示例展示了html代码的基本返回。我的路由想要将一些消息转发到应用层,这将由其他参与者实现。所以我的想法是从 ActorSystem 或 ActorContext 中检索这些演员。我不知道如何在不扩展 Actor 的情况下从特征中做到这一点。我现在正在寻找那个。
标签: scala akka spray spray-test