【发布时间】:2018-12-06 15:40:53
【问题描述】:
我在尝试“模拟”路线后面的 Actor 时遇到问题。我希望能够在测试时覆盖和模拟功能,我认为 TestProbe 是解决这个问题的正确方法。
但是,我没有得到一个 TestProbe 来接收单个请求。使用probe.expectMsg(request) 时,测试失败并显示assertion failed: timeout (3 seconds) during expectMsg while waiting for GetCardRequest(12345)。删除 expectMsg 和 reply 调用仍将导致测试失败,因为 check 块中的 Request was rejected。我期待 val result = request ~> routes ~> runRoute 能够命中底层的 TestProbe。
我觉得我只是不了解设置的简单内容!提前感谢您的帮助!
class MyRoutesSpec
extends WordSpec
with Matchers
with ScalaFutures
with ScalatestRouteTest
with MyRoutes {
lazy val routes = 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 = GetCardRequest("12345")
val cardRequestEntity = Marshal(cardRequest).to[MessageEntity].futureValue // futureValue is from ScalaFutures
val request = Post("/getcard").withEntity(cardRequestEntity)
val cardResponse = ClientCard("Hello", "World")
val result = request ~> routes ~> runRoute
probe.expectMsg(cardRequest)
probe.reply(cardResponse)
check {
status should ===(StatusCodes.Created)
contentType should ===(ContentTypes.`application/json`)
entityAs[String] should ===("""{"cardName":"Hello", "cardType":"World"}""")
} (result)
}
}
}
trait MyRoutes extends JsonSupport {
// 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") {
concat(
path(Segment) { id =>
concat(
get {
val cardFuture: Future[ClientCard] =
(cardLookupActor ? GetCardRequest(id = id)).mapTo[ClientCard]
})
})
}
}
【问题讨论】:
标签: scala akka akka-testkit