如何使用spraytestkit和HttpServiceActor进行scalatest工作
我看了
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") } } } 但是我得到了
和:
有办法解决这个问题吗? 所以我尝试更新代码,因为评论说要分成特征和服务,如: 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |