Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何使用Akka TestKit以失败响应Ask模式?_Scala_Akka_Akka Testkit - Fatal编程技术网

Scala 如何使用Akka TestKit以失败响应Ask模式?

Scala 如何使用Akka TestKit以失败响应Ask模式?,scala,akka,akka-testkit,Scala,Akka,Akka Testkit,我有一个Akka演员,他使用Ask模式从一个儿童演员那里获取未来,并对成功和失败都采取行动。我不知道如何嘲弄儿童演员并以失败作为回应 代码如下: import java.util.concurrent.TimeUnit import akka.actor.{Actor, ActorRef, ActorSystem, Props} import akka.pattern.ask import akka.testkit.{ImplicitSender, TestKitBase, TestProbe}

我有一个Akka演员,他使用Ask模式从一个儿童演员那里获取未来,并对成功和失败都采取行动。我不知道如何嘲弄儿童演员并以失败作为回应

代码如下:

import java.util.concurrent.TimeUnit
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.pattern.ask
import akka.testkit.{ImplicitSender, TestKitBase, TestProbe}
import akka.util.Timeout
import org.specs2.mutable.Specification
import org.specs2.specification.Scope
import scala.util.{Failure, Success}

class Parent(child: ActorRef) extends Actor {
  implicit val timeout = Timeout(5, TimeUnit.SECONDS)
  import context.dispatcher

  override def receive: Receive = {
    case "go" => {
      val senderRef = sender()
      (child ? "question").mapTo[String] onComplete {
        case Success("answer") =>
          senderRef ! "child responded with a successful answer"
        case Failure(throwable) =>
          senderRef ! "child responded with a failure"
      }
    }
  }
}

class SimplifiedProblemSpec extends Specification {

  "The Parent Actor" should {
    "act on success" in new TestScope {
      parent ! "go"
      childProbe.expectMsg("question")
      childProbe.reply("answer")
      expectMsg("child responded with a successful answer")
    }
    "act on failure" in new TestScope {
      parent ! "go"
      childProbe.expectMsg("question")
      // How to reply with failure?
      expectMsg("child responded with a failure")
    }
  }

}

abstract class TestScope extends Scope with TestKitBase with ImplicitSender {
  implicit lazy val system: ActorSystem = ActorSystem()

  val childProbe = TestProbe()
  val parent = system.actorOf(Props(classOf[Parent], childProbe.ref), "Parent")

}
任何帮助都将不胜感激


谢谢

您可以使用
akka.actor
包中的
Status.Failure
案例类。从文档:

此类/消息类型最好用于指示执行的某些操作失败。例如,它用于在使用AskSupport(ask/?)时发出故障信号


老实说,我不是100%确定这是你想要的,但以防万一,你可以尝试这样的东西:

case class FailureMessage(message: String) extends Exception(message)

object MyActor {
    def props = Props(classOf[MyActor])
}

class MyActor extends Actor {
    override def receive = {
        case "cool" => sender ! Status.Success("cool")
        case boom => sender ! Status.Failure(FailureMessage("boom"))
   }
}

class MyActorSpec extends TestKit(ActorSystem("system")) with ImplicitSender with WordSpecLike with Matchers {

  implicit val timeout = Timeout(1 second)

  override def afterAll = TestKit.shutdownActorSystem(system)

  "system" must {
      "return Failure if boom" in {
          val actor = system.actorOf(MyActor.props)
          actor ! "boom"
          expectMsg(Status.Failure(FailureMessage("boom")))
      }
  }