使用Scala进行Akka演员单元测试 [英] Akka actors unit testing with Scala

查看:102
本文介绍了使用Scala进行Akka演员单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Scala还是很陌生,所以请保持冷静。

I'm fairly new to Scala so please be gentle.

在我当前正在构建的应用中,我正在使用Akka演员,我想写一些单元测试。我遇到了用于编写单元测试的官方文档Akka演员

In the app I'm currently building, I'm using Akka actors and I want to write some unit tests. I came across this official documentation for writing unit tests for Akka actors

,但我不知道它应该如何工作。特别是

but I couldn't understand exactly how it should work. In particular,

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)

当我尝试这样做时,会得到:价值成功,这并不奇怪。

When I try that, I get not found: value Success, which is not surprising.

然后我发现如何测试Scala演员的示例

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)

,虽然可能已经很老了,但是它很容易理解,并且更接近我想使用期货时通常使用的东西,最重要的是可以使用。它确实需要我声明一些隐式变量,例如ActorSystem,timeout等,但官方方式似乎并非如此……

, which admittedly is possibly old, but it is easy to understand and closer to what I normally use when I want to use Futures, and most importantly works. It does require me to declare a few implicits like the ActorSystem, timeout and such, which doesn't seem to be the case with the official way...

,我想使用官方文档中建议的方法,因此如果有人可以帮助我了解它的工作原理(尤其是成功位)以及如何使用它,我将不胜感激。

If possible, I'd like to use the method proposed by the official documentation, so I would appreciate it if someone could help me understand how it works (in particular the Success bit) and how to use it.

推荐答案

您的问题的答案可能太长了,因为无法知道您实际知道多少Scala。我将尽力使答案尽可能简短,但请随时要求澄清。我还代表整个stackoverflow社区表示歉意,因为您在提出问题之前明显缺乏技巧,使您感到有道歉的必要。

The answer to your question might be too long, because it is impossible to know how much Scala you actually know. I will try to make my answer as short as possible, but do not hesitate to ask for clarification at any point. I also apologize on behalf of the whole stackoverflow community for making you feel the need to apologize due to an apparent lack of skill before asking a question.

在Scala 2.10中,一个概念引入了 Try 。它与 Option 非常相似。 Option 是处理 null s的概念。类型为 Option 的值可以采用两种形式: Some(value) None 。当您具有 Option al值时,可以对其进行模式匹配,以查看它是 Some 还是,然后采取相应行动。模式匹配发生在Scala的许多地方,其中之一是在 val s初始化期间。以下是一些示例:

In Scala 2.10 a concept of Try was introduced. It is very similar to Option. Option is a concept of handling nulls. A value of type Option can take two forms: Some(value) or None. When you have an Optional value you can pattern match on it to see if it is a Some or a None and then act accordingly. Pattern matching occurs in many places in Scala and one of them is during the initialization of vals. Here are few examples:

val x = 10 // pattern 'x' on the LHS matches any value on the RHS so 'x' is initialized with 10
val Some(x) = Some(10) // pattern 'Some(x)' on the LHS matches any value of type 'Some' and binds it's value to x, so 'x' is yet again initialized with 10

尝试进行初始化是处理异常的概念。类型 Try 的值可以采用两种形式:成功(结果) Failure(可抛出) )。当您的值类型为 Try 时,可以对其进行模式匹配,以查看它是成功还是失败

Try is a concept of handling exceptions. A value of type Try can take two forms: Success(result) or Failure(throwable). When you have a value of type Try you can pattern match on it to see if it is a Success or a Failure.

这就是代码中发生的情况(成功的模式匹配)。与 Option 相比, Try 的两种形式默认不在范围内,这会导致编译错误。可以解决此问题:

This is what happens in your code (pattern matching on Success). In contrast to Option the two forms of Try are not in scope by default, which causes the compilation error. This will fix it:

import scala.util.{Try, Success, Failure}

这篇关于使用Scala进行Akka演员单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆