我看了spray 1.3.1 testkit documentation,但找不到下面我需要的正确例子:
我有这个示例spray 1.3.1
服务
trait MyService extends HttpServiceActor {
def receive = runRoute(routes)
val isAliveRoute = path("isalive") {
get {
complete("YES")
}
}
val routes = isAliveRoute
}
我尝试使用spray test-kit
进行测试,但未能这样做是TestCase
@RunWith(classOf[JUnitRunner])
class MyServiceTest extends FlatSpec with ScalatestRouteTest with ShouldMatchers with MyService {
"The service" should "return a greeting for GET requests to the isalive" in {
Get() ~> isAliveRoute ~> check {
responseAs[String] should be("YES")
}
}
}
但是我得到了
错误:(15,87)非法继承;超类FlatSpec不是 mixin特征的超类HttpServiceActor的子类 MyService类MyServiceTest使用ScalatestRouteTest扩展FlatSpec 使用带有MyService的ShouldMatchers { ^
^
和
错误:(17,11)找不到参数ta的隐含值: MyServiceTest.this.TildeArrow [spray.routing.RequestContext,单元] Get()〜> isAliveRoute~>检查{ ^
有办法解决这个问题吗?
我可以使用我的服务extend HttpServiceActor
并仍然可以使用scalatest和spray testkit进行测试吗?如果是这样的话?我想继续扩展HttpServiceActor使生活更轻松,代码更紧凑和可读。但我也想用最新的测试它。
所以我尝试更新代码,因为评论说要分成特征和服务,如: https://github.com/spray/spray-template/blob/on_spray-can_1.1/src/main/scala/com/example/MyService.scala
class MyServiceActor extends Actor with MyService {
def actorRefFactory = context
def receive = runRoute(routes)
}
trait MyService extends HttpService {
val isAliveRoute = path("isalive") {
get {
complete("OK")
}
}
val routes = isAliveRoute
}
@RunWith(classOf[JUnitRunner])
class MyServiceTest extends FlatSpec with ShouldMatchers with MyService with ScalatestRouteTest {
def actorRefFactory = system
"The service" should "return a greeting for GET requests to the isalive" in {
Get() ~> isAliveRoute ~> check {
responseAs[String] should be("YES")
}
}
}
但我明白了:
测试于13:26开始...... [调查] [2014年5月14日13:26:25.813] [ScalaTest运行] [EventStream(akka:// com-server-web-conf-MyServiceTest)]记录器 log1-Logging $ DefaultLogger started [DEBUG] [05/14/2014 13:26:25.814] [ScalaTest运行] [EventStream(akka:// com-server-web-conf-MyServiceTest)]默认 记录器启动请求未被处理 org.scalatest.exceptions.TestFailedException:未处理请求 在 spray.testkit.ScalatestInterface $ class.failTest(ScalatestInterface.scala:25) 在
答案 0 :(得分:2)
我有一个差异的类似问题。在完整的声明中,我已经向另一个演员发送消息,所以我需要演员功能来测试行为。我这样解决了:
trait MyService extends HttpService {
val myActor: ActorRef
val homeS: ActorRef
(...)
并在里面发送消息到
path("isalive") { get {
ctx: RequestContext => homeS.tell(ctx, myActor ) }
//on homeS actor:
def receive = {
case ctx: RequestContext =>
ctx.complete( ... )
但如果您不需要MyService中的actor功能,那么最好像@jrudolph那样在评论中说。
此处的完整代码:https://github.com/kastoestoramadus/simple_zookeeper.git