加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何使用spraytestkit和HttpServiceActor进行scalatest工作

发布时间:2020-12-16 18:33:33 所属栏目:安全 来源:网络整理
导读:我看了 spray 1.3.1 testkit documentation,但找不到下面我需要的一个正确的例子: 我有这个样品喷雾1.3.1服务 trait MyService extends HttpServiceActor { def receive = runRoute(routes) val isAliveRoute = path("isalive") { get { complete("YES") }
我看了 spray 1.3.1 testkit documentation,但找不到下面我需要的一个正确的例子:
我有这个样品喷雾1.3.1服务

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

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

我正在尝试使用喷雾测试套件进行测试,但未能这样做是我的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")
    }
  }
}

但是我得到了

Error:(15,87) illegal inheritance; superclass FlatSpec is not a
subclass of the superclass HttpServiceActor of the mixin trait
MyService class MyServiceTest extends FlatSpec with ScalatestRouteTest
with ShouldMatchers with MyService {
^
^

和:

Error:(17,11) could not find implicit value for parameter ta:
MyServiceTest.this.TildeArrow[spray.routing.RequestContext,Unit]
Get() ~> isAliveRoute ~> check {
^

有办法解决这个问题吗?
我可以使用我的服务扩展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")
    }
  }
}

但我得到:

Testing started at 13:26 … [DEBUG] [05/14/2014 13:26:25.813]
[ScalaTest-run]
[EventStream(akka://com-server-web-conf-MyServiceTest)] logger
log1-Logging$DefaultLogger started [DEBUG] [05/14/2014 13:26:25.814]
[ScalaTest-run]
[EventStream(akka://com-server-web-conf-MyServiceTest)] Default
Loggers started Request was not handled
org.scalatest.exceptions.TestFailedException: Request was not handled
at
spray.testkit.ScalatestInterface$class.failTest(ScalatestInterface.scala:25)
at

解决方法

我有一个差异的类似问题.在完整的声明中,我向另一个演员发送了消息,因此我需要演员功能来测试行为.我这样解决了:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读