Reputation: 20222
I have the following test for an Akka component:
import somePackage.SomeActor
import akka.actor.{ActorSystem, Props}
import org.scalatest.{FlatSpec, Matchers}
class SomeActorSpec extends FlatSpec with Matchers {
val system = ActorSystem()
val someActorRef = system.actorOf(Props(classOf[SomeActor]))
it should "check the id" in {
someActorRef ! CheckIfJobIsRunning(UUID.randomUUID)
expectMsg(SomeOtherMessage(List()))
}
}
I get the error:
not found: value expectMsg
[error] expectMsg(SomeOtherMessage(List()))
I have two questions:
1.How can I use expectMsg
?
2.Where do I define, SomeOtherMessage
, which should be received by the test class?
Upvotes: 1
Views: 113
Reputation: 4515
Use TestKit
scope. Your example should look like:
class SomeActorSpec extends FlatSpec with Matchers {
it should "check the id" in new Scope {
someActorRef ! CheckIfJobIsRunning(UUID.randomUUID)
expectMsg(SomeOtherMessage(List()))
}
abstract class Scope extends TestKit(ActorSystem()) {
val someActorRef = system.actorOf(Props(classOf[SomeActor]))
}
}
More info in docs. Regarding your questions:
expectMsg
to test that actor sends SomeOtherMessage
to some other actor when it receives CheckIfJobIsRunning
CheckIfJobIsRunning
, SomeOtherMessage
and others in some Protocol-like file which contains all messages related to your actor. I personally use companion object of actor to specify all these messasges.Upvotes: 2