gpt4 book ai didi

testing - 如何测试 Actor ?

转载 作者:行者123 更新时间:2023-11-28 21:28:10 25 4
gpt4 key购买 nike

我有一个 Actor ,它在其中一条消息上运行这个方法:

def addAuctions(actions:List[String]): Unit = {
var i = 0
for(auction <- actions) {
val x = context.actorOf(Props(new Auction(auction, AUCTION_LIMIT_IN_SECONDS, self)), "auction" + i)
x ! Auction.Init
i= i+1
}

然后我有以下测试:

"seller" in {
val seller = system.actorOf(Props[Seller],"seller")
seller ! Seller.NewAuctions(List("Laptop"))

// HERE - HOW TO TEST IT ?

}

如何测试卖家是否发送消息 Auction.Init ?

最佳答案

Akka site 上有一个关于测试 actors 的好教程。 .

实现您想要的目标的一种直接方法是将创建委托(delegate)参与者的代码提取到它自己的方法中,然后用测试探针覆盖它。因此,您可以按照以下方式做一些事情。

class Foo extends Actor {
// Suppose this is called after receiving a message called DoSomething
def doSomething(): Unit = delegateActor ! SomeMsg
def delegateActor: ActorRef = context.actorOf(Props(...))
}

在你的测试代码中,

class TestFoo(dActor: ActorRef) extends Foo {
override def delegateActor = dActor
}

"test" in {
val probe = TestProbe()
val foo = TestActorRef(new TestFoo(probe))
foo ! DoSomething
probe.expectMsg(SomeMsg)
}

关于testing - 如何测试 Actor ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33491912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com