【发布时间】:2019-01-01 22:13:02
【问题描述】:
我正在尝试测试这个简单的演员:
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")
}
}
我收到此错误:
- should e f g * FAILED * [info] java.lang.AssertionError: assertion failed: timeout (3 seconds) during expectMsg while waiting 在 scala.Predef$.assert(Predef.scala:170) 处发送(123)[信息] [信息] 在 akka.testkit.TestKitBase$class.expectMsg_internal(TestKit.scala:402) [信息] 在 akka.testkit.TestKitBase$class.expectMsg(TestKit.scala:379) [信息]
在 akka.testkit.TestKit.expectMsg(TestKit.scala:896) [信息] 在 TestKitUsageSpec$$anonfun$1$$anonfun$apply$mcV$sp$5.apply(ActorSpec.scala:49) [信息] 在 TestKitUsageSpec$$anonfun$1$$anonfun$apply$mcV$sp$5.apply(ActorSpec.scala:47) [信息] 在 org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85) [信息]
在 org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104) [信息] 在 org.scalatest.Transformer.apply(Transformer.scala:22) [信息] 在 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))
}
}
【问题讨论】:
-
看起来您正在测试
Notify将其Send消息回显给发件人 (expectMsg(Notify.Send(123))),但所有Notify参与者所做的只是记录其收到的 @987654327 @消息而什么都不做。因此,expectMsg调用超时,因为没有发回任何内容,在此过程中产生异常。