我正在尝试测试这个简单的演员:
object Notify {
def props(incidentId: Int): Props = Props(new Notify(incidentId: Int))
final case class Send(reportId: Int)
}
class Notify(incidentId: Int) extends Actor with ActorLogging {
import Notify._
log.info("Notify constructor...")
// val x = 0
// val y = 123 / x
override def receive: Receive = {
case Send(reportId) =>
log.debug(s"Notify Send $reportId")
}
}
我收到此错误:
- 应该 *失败* [info] java.lang.AssertionError:断言失败:在等待期间的ExpectMsg中超时(3秒) 用于scala.Predef $ .assert(Predef.scala:170)的Send(123)[info] [info]位于 akka.testkit.TestKitBase $ class.expectMsg_internal(TestKit.scala:402) [info]位于 akka.testkit.TestKitBase $ class.expectMsg(TestKit.scala:379)[info]
在akka.testkit.TestKit.expectMsg(TestKit.scala:896)[info]在 TestKitUsageSpec $$ anonfun $ 1 $$ anonfun $ apply $ mcV $ sp $ 5.apply(ActorSpec.scala:49) [info]位于 TestKitUsageSpec $$ anonfun $ 1 $$ anonfun $ apply $ mcV $ sp $ 5.apply(ActorSpec.scala:47) [info]位于 org.scalatest.OutcomeOf $ class.outcomeOf(OutcomeOf.scala:85)[info]
在org.scalatest.OutcomeOf $ .outcomeOf(OutcomeOf.scala:104)处[信息]在 org.scalatest.Transformer.apply(Transformer.scala:22)[info]位于 org.scalatest.Transformer.apply(Transformer.scala:20)
我的Akka测试设置如下:
import scala.util.Random
import org.scalatest.BeforeAndAfterAll
import org.scalatest.WordSpecLike
import org.scalatest.Matchers
import com.typesafe.config.ConfigFactory
import akka.actor.Actor
import akka.actor.ActorRef
import akka.actor.ActorSystem
import akka.actor.Props
import akka.testkit.{ TestActors, DefaultTimeout, ImplicitSender, TestKit, TestProbe }
import scala.concurrent.duration._
import scala.collection.immutable
import com.example.notifications._
class TestKitUsageSpec
extends TestKit(ActorSystem(
"TestKitUsageSpec",
ConfigFactory.parseString(TestKitUsageSpec.config)))
with DefaultTimeout with ImplicitSender
with WordSpecLike with Matchers with BeforeAndAfterAll {
import TestKitUsageSpec._
val echoRef = system.actorOf(TestActors.echoActorProps)
val forwardRef = system.actorOf(Props(classOf[ForwardingActor], testActor))
val filterRef = system.actorOf(Props(classOf[FilteringActor], testActor))
val randomHead = Random.nextInt(6)
val randomTail = Random.nextInt(10)
val headList = immutable.Seq().padTo(randomHead, "0")
val tailList = immutable.Seq().padTo(randomTail, "1")
val seqRef =
system.actorOf(Props(classOf[SequencingActor], testActor, headList, tailList))
val notifyActor = system.actorOf(Notify.props(123))
override def afterAll {
shutdown()
}
"a b c d " should {
"e f g" in {
notifyActor ! Notify.Send(123)
expectMsg(Notify.Send(123))
}
}
答案 0 :(得分:3)
在测试中,您要向演员(expectMsg(Notify.Send(123))
)发送一条消息,然后测试您的演员是否以相同的消息(sender ! Notify.Send(123)
)进行响应。
您的示例演员没有响应请求。因此,您的测试会放弃在某个时间等待超时异常的响应。
要运行测试,必须让参与者响应请求。 您可以通过添加到接收方法{{1}}中来做到这一点。