Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.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
Akka(Scala)-使用参数iwth TestKit测试消息_Scala_Akka - Fatal编程技术网

Akka(Scala)-使用参数iwth TestKit测试消息

Akka(Scala)-使用参数iwth TestKit测试消息,scala,akka,Scala,Akka,在我的应用程序中,我有一个CartActor管理购物车,OrderManager监督整个购物过程: object CartActor { sealed trait Command case class AddItem(item: Any) extends Command case class RemoveItem(item: Any) extends Command case object ExpireCart extends

在我的应用程序中,我有一个CartActor管理购物车,OrderManager监督整个购物过程:

object CartActor {

  sealed trait Command
  case class AddItem(item: Any)        extends Command
  case class RemoveItem(item: Any)     extends Command
  case object ExpireCart               extends Command
  case object StartCheckout            extends Command
  case object ConfirmCheckoutCancelled extends Command
  case object ConfirmCheckoutClosed    extends Command
  case object GetItems                 extends Command // command made to make testing easier

  sealed trait Event
  case class CheckoutStarted(checkoutRef: ActorRef) extends Event

  def props() = Props(new CartActor())
}
OrderManager从CartActor接收
OrderManager.ConfirmCheckoutStarted(checkoutRef:ActorRef)
。我必须编写单元测试来检查这一点。我想做:

  it should "start checkout" in {
    val cart = TestActorRef[CartActor]

    cart ! AddItem("Item")
    cart ! StartCheckout
    expectMsg(OrderManager.ConfirmCheckoutStarted)
  }

但我得到一个错误:
断言失败:预期ConfirmCheckoutStarted,发现ConfirmCheckoutStarted(Actor[akka://CartTest/user/$$d/结帐#-1922766677])
。我发现问题在于,偏执论中有一个参数。我如何测试这个?我不在乎里面的actorRef,我只想确认CheckOutstarted

我不知道tour actor会发回什么消息。因此,我创建了这个
CartActor
发送消息
OrderManager.ConfirmCheckoutStarted(self)

然后,我执行此测试,检查参与者引用:

import akka.actor.ActorSystem
import akka.testkit.{ImplicitSender, TestActorRef, TestKit}
import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

class CartActorDemoSpec extends TestKit(ActorSystem("CartActorDemoSpec"))
  with ImplicitSender
  with AnyWordSpecLike
  with Matchers
  with BeforeAndAfterAll {

  override def afterAll(): Unit = {
    TestKit.shutdownActorSystem(system)
  }

  import CartActorDemo._

  "The ActorsIntro actor" should {
    "send back hi replay" in {
      import CartActorDemo.CartActor._
      val cart = TestActorRef[CartActor]

      val item = "MyItem"
      cart ! AddItem(item)
      expectMsg(s"item $item added.")
      cart ! StartCheckout
      expectMsg(OrderManager.ConfirmCheckoutStarted(cart))
    }
  }
}

如果您有另一种类型的消息发送回您的
案例开始签出
,您可以相应地更改它。

checkoutRef:ActorRef
包装在
选项中
并检查
ConfirmCheckoutStarted(Some())
@Felipe这是大学作业,我不允许更改它。我只需要为这些类编写测试。
  class CartActor extends Actor {

    import CartActor._

    override def receive: PartialFunction[Any, Unit] = {
      case AddItem(item) => sender ! s"item $item added."
      case StartCheckout => sender ! OrderManager.ConfirmCheckoutStarted(self)
      case msg => println(s"I cannot understand ${msg.toString}")
    }
  }
import akka.actor.ActorSystem
import akka.testkit.{ImplicitSender, TestActorRef, TestKit}
import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

class CartActorDemoSpec extends TestKit(ActorSystem("CartActorDemoSpec"))
  with ImplicitSender
  with AnyWordSpecLike
  with Matchers
  with BeforeAndAfterAll {

  override def afterAll(): Unit = {
    TestKit.shutdownActorSystem(system)
  }

  import CartActorDemo._

  "The ActorsIntro actor" should {
    "send back hi replay" in {
      import CartActorDemo.CartActor._
      val cart = TestActorRef[CartActor]

      val item = "MyItem"
      cart ! AddItem(item)
      expectMsg(s"item $item added.")
      cart ! StartCheckout
      expectMsg(OrderManager.ConfirmCheckoutStarted(cart))
    }
  }
}