Scala TestProbe未从RouteSpec接收消息

Scala TestProbe未从RouteSpec接收消息,scala,akka,akka-testkit,Scala,Akka,Akka Testkit,当我试图在路线后面“模仿”一个演员时,我遇到了一些问题。我希望能够在测试时覆盖和模拟功能,我认为TestProbe是实现这一点的正确方法 然而,我还没有得到一个TestProbe来接收一个请求。使用probe.expectMsg(request)时,测试失败,因为断言失败:等待GetCardRequest(12345)时,expectMsg期间超时(3秒)。删除expectMsg和reply调用仍将导致测试失败,因为请求在检查块被拒绝。我希望val result=request~>routes~

当我试图在路线后面“模仿”一个演员时,我遇到了一些问题。我希望能够在测试时覆盖和模拟功能,我认为TestProbe是实现这一点的正确方法

然而,我还没有得到一个TestProbe来接收一个请求。使用
probe.expectMsg(request)
时,测试失败,因为
断言失败:等待GetCardRequest(12345)时,expectMsg期间超时(3秒)
。删除
expectMsg
reply
调用仍将导致测试失败,因为
请求在
检查
块被拒绝。我希望
val result=request~>routes~>runRoute
命中底层TestProbe

我觉得我只是不了解一些简单的设置!提前谢谢你的帮助

类MyRouteSpec
扩展WordSpec
和媒人
有规模的未来
用ScalatestRouteTest
使用MyRoutes{
惰性val路由=MyRoutes
val probe=新的TestProbe(系统)
覆盖val cardLookupActor=probe.ref
//要弄清楚如何让TestProbe真正工作!!
“我的路线”应该{
在中的“能够从请求中获得卡”{
val cardRequest=GetCardRequest(“12345”)
val cardRequestEntity=Marshal(cardRequest).to[MessageEntity].futureValue//futureValue来自ScalaFutures
val请求=Post(“/getcard”)。带有实体(cardRequestEntity)
val cardResponse=ClientCard(“你好”,“世界”)
val result=request~>routes~>runRoute
probe.expectMsg(cardRequest)
调查回复(cardResponse)
检查{
状态应==(StatusCodes.Created)
contentType应该==(ContentTypes.`application/json`)
entityAs[String]应该==(“{”cardName:“你好”,“cardType:“世界”}”)
}(结果)
}
}
}
trait MyRoutes扩展了JsonSupport{
//我们留下这些摘要,因为它们将由应用程序提供
隐式def系统:ActorSystem
lazy val log=日志记录(系统,类[MyRoutes])
//路由使用的其他依赖项
def cardLookupActor:ActorRef
//下面的“询问”(?)方法要求
隐式延迟val超时=超时(5秒)
惰性val myRoutes:路由=
路径前缀(“getcard”){
海螺(
路径(段){id=>
海螺(
得到{
val卡未来:未来[客户卡]=
(cardLookupActor?GetCardRequest(id=id)).mapTo[ClientCard]
})
})
}
}

您的测试工作正常,但它只是证明了永远不会调用测试探针

检查通过的测试是否正常

import akka.Done
import akka.actor.ActorRef
import akka.actor.ActorSystem
import akka.event.Logging
import akka.http.scaladsl.server._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.testkit.ScalatestRouteTest
import akka.testkit.TestProbe
import akka.util.Timeout
import org.scalatest.Matchers
import org.scalatest.WordSpec
import org.scalatest.concurrent.ScalaFutures
import akka.pattern.ask

import concurrent.duration._

class MyRoutesSpec extends WordSpec with Matchers with ScalaFutures with ScalatestRouteTest with MyRoutes {

  val probe = new TestProbe(system)
  override val cardLookupActor = probe.ref

  //TODO figure out how to get TestProbe to actually work!!
  "My Routes" should {
    "be able to get a card from a request" in {
      val cardRequest = "12345"
      val request = Get("/getcard/sss").withEntity(cardRequest)
      val cardResponse = "HelloWorld"

      val result = request ~> myRoutes ~> runRoute

      probe.expectMsg(Done)
      probe.reply(cardResponse)

      check {
        status should ===(StatusCodes.OK)
        entityAs[String] should ===("""HelloWorld""")
      }(result)
    }
  }
}

trait MyRoutes {

  // we leave these abstract, since they will be provided by the App
  implicit def system: ActorSystem

  lazy val log = Logging(system, classOf[MyRoutes])

  // other dependencies that Routes use
  def cardLookupActor: ActorRef

  // Required by the `ask` (?) method below
  implicit lazy val timeout = Timeout(5.seconds)

  lazy val myRoutes: Route =
    pathPrefix("getcard") {
      path(Segment) { _ =>
        get {
          complete((cardLookupActor ? Done).mapTo[String])
        }
      }
    }
}
什么固定的

  • lazy val routes=MyRoutes
    已删除
  • 您发送
    Post
    ,但路由需要
    Get
  • 测试中没有通过的段,即路径前缀(“getcard”){path(segment){id=>?}中的
    /getcard
    不匹配
  • 调用
    request~>myRoutes~>runRoute
    而不是
    request~>routes~>runRoute
  • 我删除了case类只是为了能够运行它