如何使用spraytestkit和HttpServiceActor使scalatest工作
我查看了以下内容,但找不到合适的示例: 我有这个样本如何使用spraytestkit和HttpServiceActor使scalatest工作,scala,akka,spray,Scala,Akka,Spray,我查看了以下内容,但找不到合适的示例: 我有这个样本spray 1.3.1服务 trait MyService extends HttpServiceActor { def receive = runRoute(routes) val isAliveRoute = path("isalive") { get { complete("YES") } } val routes = isAliveRoute } 我正试图用喷雾测试套件对其进行测试,但
spray 1.3.1
服务
trait MyService extends HttpServiceActor {
def receive = runRoute(routes)
val isAliveRoute = path("isalive") {
get {
complete("YES")
}
}
val routes = isAliveRoute
}
我正试图用喷雾测试套件对其进行测试,但未能做到这一点,这里是我的测试用例
@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 trait的超类HttpServiceActor的子类
MyService类MyServiceTest使用ScalateStroutTest扩展了FlatSpec
使用MyService的ShouldMatchers{
^
^
以及:
错误:(17,11)找不到参数ta的隐式值:
MyServiceTest.this.TildeArrow[spray.routing.RequestContext,Unit]
Get()~>isAliveRoute~>检查{
^
有办法解决这个问题吗?
我可以让我的服务扩展HttpServiceActor
,并且仍然能够使用scalatest和spray testkit对其进行测试吗?如果可以,如何进行?我想继续扩展HttpServiceActor,使工作更轻松,代码更紧凑,可读性更强。但我也想使用scalatest对其进行测试
因此,我尝试更新代码,如注释所述,拆分为trait和service,如下所示:
但我得到:
测试于13:26开始…[调试][05/14/2014 13:26:25.813]
[缩放测试运行]
[EventStream(akka://com-server-web-conf-MyServiceTest)]记录器
log1日志$DefaultLogger已启动[调试][05/14/2014 13:26:25.814]
[缩放测试运行]
[EventStream(akka://com-server-web-conf-MyServiceTest)]违约
未处理记录器启动的请求
org.scalatest.exceptions.TestFailedException:未处理请求
在
ScalatestInterface$class.failTest(ScalatestInterface.scala:25)
在
我有一个类似的问题,但有一个不同。在完成声明时,我向另一个参与者发送了消息,所以我需要参与者功能来测试行为。我这样解决了这个问题:
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在评论中说的那个样做
这里的完整代码:如果您将代码拆分为路由部分(仅从HttpService
扩展)并将与参与者相关的内容移动到实现参与者的具体类中,那么它会起作用。请参见spray模板中的操作方法:@jrudolph updated question,我尝试过它有帮助,但我仍然有一个错误……这是一个真正的问题e测试失败,因为您的请求(Get()
)没有处理空路径的URL。如果您希望在这种情况下生成实际的失败响应(404),您必须使用Get()~>sealRoute(isAliveRoute)~>…
@jrudolph谢谢!已回答!@jrudolph可以作为回答发布。。
path("isalive") { get {
ctx: RequestContext => homeS.tell(ctx, myActor ) }
//on homeS actor:
def receive = {
case ctx: RequestContext =>
ctx.complete( ... )