However, if that is not the case then TestKit has a very simple way you can do it with the TestActorRef and a TestProbe. Any where you have a piece of code that looks like this:
context.parent ! "Any message"
For example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChildActor extends Actor { | |
def receive: Actor.Receive = { | |
case "Do something" => { | |
// Do something important | |
context.parent ! "I was told to do something and I did it" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChildActorTest extends TestKit(ActorSystem("ChildActorTest")) with FunSuiteLike with ShouldMatchers { | |
test("Should inform parent when told to do something") { | |
val parent = TestProbe() | |
val underTest = TestActorRef(Props[ChildActor], parent.ref, "ChildActor") | |
underTest ! "Do something" | |
parent.expectMsg("I was told to do something and I did it") | |
} | |
} |
Full code here. Happy testing.
3 comments:
Thanks, this helped a lot. I am using Java, but still I was able to test my parent/child.
thanks, really helpful
Thanks heaps for sharing that. I was scratching my head as to why sender ! Message worked but couldn't get the parent ! Message test working.
Post a Comment