我查看了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")
}
}
}
但是我明白了
和:
有办法解决吗?
我可以使用
extend HttpServiceActor
服务,并且仍然可以使用scalatest和spray testkit对其进行测试吗?如果是这样怎么办?我想继续扩展HttpServiceActor,使生活更轻松,代码更紧凑,更易读。但我也想用scalatest对其进行测试。所以我尝试按照注释所说的那样更新代码,以拆分为特征和服务,如下所示:
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")
}
}
}
但我得到:
最佳答案
我有一个相似的问题,但有一个区别。在完整的声明中,我已经向其他角色发送了消息,因此我需要角色功能来测试行为。我是这样解决的:
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
关于scala - 如何使用Spraytestkit和HttpServiceActor使Scalatest正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23649081/