【发布时间】:2013-05-23 02:14:12
【问题描述】:
我对 Scala 还很陌生,所以请保持温和。
在我目前正在构建的应用程序中,我正在使用 Akka 演员,我想编写一些单元测试。我遇到了这个official documentation for writing unit tests for Akka actors
但我无法准确理解它应该如何工作。特别是,
val actorRef = TestActorRef(new MyActor)
// hypothetical message stimulating a '42' answer
val future = actorRef ? Say42
val Success(result: Int) = future.value.get
result must be(42)
当我尝试这样做时,我得到了not found: value Success,这并不奇怪。
然后我找到了this example of how to test Scala actors
val actorRef = TestActorRef[TickTock]
implicit val timeout = Timeout(5 seconds)
val future = (actorRef ? new Tick("msg")).mapTo[String]
val result = Await.result(future, timeout.duration)
Assert.assertEquals("processed the tick message", result)
,诚然,它可能已经过时了,但它很容易理解,并且更接近我想使用 Futures 时通常使用的东西,而且最重要的是有效。它确实需要我声明一些隐式,例如 ActorSystem、超时等,而官方的方式似乎并非如此......
如果可能的话,我想使用官方文档提出的方法,所以如果有人能帮助我理解它是如何工作的(特别是 Success 位)以及如何使用它,我将不胜感激。
【问题讨论】:
-
你有像
scala.util._或scala.util.Success这样的导入吗?
标签: scala testing akka actor specs