我正在尝试为我的演员编写单元测试,并且我坚持基本的嘲弄。 PriceAggregateActor正在使用akka持久性,我不想传递所有的conf,并想完全嘲笑它。
这是我要测试的演员
object CommandPriceActor {
def apply() = Props(classOf[CommandPriceActor], PriceAggregateActor())
}
class CommandPriceActor(priceAggregateActorProps: Props) extends Actor with ActorLogging {
val priceAggregateActor = context.actorOf(priceAggregateActorProps, "priceAggregateActor")
因此,在我的测试中,我尝试做类似的事情:
class CommandPriceActorTest extends TestKit(ActorSystem("test-benefits",
ConfigFactory.parseString("""akka.loggers = ["akka.testkit.TestEventListener"] """))) with FlatSpecLike with Matchers
with BeforeAndAfterAll with Eventually{
class MockedChild extends Actor {
def receive = {
case _ => lala
}
}
val probe = TestProbe()
val commandPriceActor = TestActorRef(new CommandPriceActor(Props[MockedChild]))
我总是得到:
Caused by: java.lang.IllegalArgumentException: no matching constructor found on class CommandPriceActorTest$MockedChild for arguments []
为什么抱怨mockedChild?它不应该采用任何构造函数参数。
答案 0 :(得分:1)
这是因为MockedChild是您测试的儿童演员。缺少的构造函数参数是对测试的引用(它是父类)。
您有三种选择:
this
的引用传递给Props
Props
选项1
val probe = TestProbe()
val mockProps = Props(classOf[MockedChild], this)
val commandPriceActor = TestActorRef(new CommandPriceActor(mockProps))
选项2
val probe = TestProbe()
val mockProps = Props(new MockedChild)
val commandPriceActor = TestActorRef(new CommandPriceActor(mockProps))
选项3
val probe = TestProbe()
val mockProps = Props(new CommandPriceActorTest.MockedChild)
val commandPriceActor = TestActorRef(new CommandPriceActor(mockProps))
// ....
object CommandPriceActorTest {
class MockedChild extends Actor {
def receive = {
case _ => lala
}
}
}