注射Akka的TestProbe代替儿童演员

时间:2014-11-03 20:23:47

标签: scala unit-testing akka probe

我有一个名为"经理"的父母演员。这创造了几个儿童演员。 这些儿童演员然后通过"发件人告诉"发回他们的回复,即直接回到"经理"。

我想为这个管理员演员创建一个单元测试,因此需要注入一个探测器来将管理器中的消息转发给它的子节点。

我使用了以下帖子: http://www.superloopy.io/articles/2013/injecting-akka-testprobe.html

但是我仍然无法正确完成此操作。

为了简化这种情况,附上的代码描述了我为一个孩子写的演员和单元测试。

经理班:

trait ManagerChildProvider {
  def createTimestampPointChild: Actor
}

trait ProductionManagerChildProvider extends ManagerChildProvider {
  def createTimestampPointChild = new TimeDifferenceCalculationActor 
}

object Manager {
  def apply() = new Manager("cid1") with ProductionManagerChildProvider
}

class Manager(name: String) extends Actor with ActorLogging {

  this: ManagerChildProvider =>

  @Autowired private val modelParams = new ModelParams //list of  parameters

  val timeDifference = context.actorOf(Props(createTimestampPointChild))

  def receive = {
    case p@TimePoint(tPoint) =>
      timeDifference ! p
    case _ =>
      log.error("Unknown message type")
  }
}

儿童班:

class TimeDifferenceCalculationActor extends Actor with ActorLogging {

  var previousTimestamp: Long = -1

  def receive = {
    case tPoint(timestamp) =>
          if (previousTimestamp != -1) {
            sender ! Result(1)
          }
    case _ =>
      log.error("Unknown message type")
  }
}

测试类:

object BarSpec {
  class Wrapper(target: ActorRef) extends Actor {
    def receive = {
      case x => target forward x
    }
  }
}

trait ChildrenProvider {
  def newFoo: Actor
}

class BarSpec extends TestKitSpec("BarSpec") {

  import Manager._
  import BarSpec._

trait TestCase {
    val probe = TestProbe()

    trait TestChildrenProvider extends ManagerChildProvider {
      def newBar = new Wrapper(probe.ref)
    }

    val actor = system.actorOf(Props(new Manager(componentId = "cid1") with TestChildrenProvider))
  }

  "Bar" should {
    "involve child in doing something" in new TestCase {

      actor ! tPoint(1)
      actor ! tPoint(2)

      probe.expectMsg(tPoint(1))
  //probe.reply("ReplyFromChild")
  //expectMsg("ReplyFromParent")
    }
  }
}

附加测试类:

abstract class TestKitSpec(name: String) extends TestKit(ActorSystem(name)) with MustMatchers with BeforeAndAfterAll with ImplicitSender with WordSpecLike{

override def afterAll() {
    system.shutdown()
  }
}

目前我收到以下错误:

  

错误:(36,42)对象创建是不可能的,因为方法>特权ManagerChildProvider中的createTimestampPointChild> type => akka.actor.Actor未定义       val actor = system.actorOf(Props(新管理器(componentId =" cid1")和TestChildrenProvider))

     

错误:(11,16)覆盖方法在trait中运行BeforeAndAfterAll类型> (testName:Option [String],args:> org.scalatest.Args)org.scalatest.Status;    方法运行在特性WordSpecLike类型(testName:Option [String],> args:org.scalatest.Args)org.scalatest.Status需要`abstract override' >修饰符   抽象类TestKitSpec(name:String)extends> TestKit(ActorSystem(名))

任何有关这些特定错误或一般任务的帮助都将受到高度赞赏

0 个答案:

没有答案