本文介绍了如何使用spraytestkit和HttpServiceActor使scalatest工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了,但是找不到合适的示例我在下面需要:
我有此示例喷涂1.3.1 服务

I looked at spray 1.3.1 testkit documentation but could not find a proper example for what I need below:I have this sample spray 1.3.1 service

trait MyService extends HttpServiceActor {
  def receive = runRoute(routes)

  val isAliveRoute = path("isalive") {
    get {
        complete("YES")
    }
  }
  val routes = isAliveRoute
}

我正在尝试使用喷雾测试工具包对其进行测试,但是没有这样做,这是我的 TestCase

I'm trying to test it with spray test-kit but failing to do so here is my 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")
    }
  }
}

但是我得到

和:

有没有办法解决?
我可以将我的服务扩展HttpServiceActor 并仍然可以使用scalatest和spray testkit对其进行测试吗?如果是这样怎么办?我想继续扩展HttpServiceActor,使生活更轻松,代码更紧凑,更易读。但是我也想用scalatest对其进行测试。

Are there ways around this?Can I have my service extend HttpServiceActor and still be able to test it with scalatest and spray testkit? if so how? I want to continue extending HttpServiceActor makes life easier and code more compact and readable. But I would also like to test it with scalatest.

所以我尝试更新代码,因为注释被分割为特征和服务,如:

so i tried updating the code as comment said to split to trait and service as in: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")
    }
  }
}

但我得到:


推荐答案

我有一个相似的问题,只是有一个区别。在完整的声明中,我已经向其他角色发送了消息,因此我需要角色功能来测试行为。我是这样解决的:

I had similar problem with one difference. At complete statement I had sending message to another actor, so I needed actor functionality to test behavior. I solved it that way:

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中,最好是像@jrudolph在评论中说的那样。

but if you don't need actor functionality of in MyService then better is to do like @jrudolph said in comment.

完整代码在这里:

这篇关于如何使用spraytestkit和HttpServiceActor使scalatest工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 11:19