Last active
August 29, 2015 14:10
-
-
Save FloWi/55572324f2d3adb3d51b to your computer and use it in GitHub Desktop.
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
package utils | |
import akka.actor.{Actor, ActorSystem} | |
import akka.testkit.{ImplicitSender, TestActorRef, TestKit} | |
import org.scalatest.FunSuiteLike | |
import scala.concurrent.duration._ | |
class StatefulnessBetweenTestsWithImplicitSenderSpec extends TestKit(ActorSystem("MyTestSystem")) | |
with FunSuiteLike | |
with ImplicitSender { | |
test("1st test") { | |
val testActorRef: TestActorRef[MyActorWithState] = TestActorRef[MyActorWithState] | |
testActorRef ! "Hello" | |
//sending the message and watching asserting the right state of the underlying actor | |
//the answer is not being consumed by some expect-method | |
assert(testActorRef.underlyingActor.state == "Hello") | |
} | |
test("2nd test") { | |
val testActorRef: TestActorRef[MyActorWithState] = TestActorRef[MyActorWithState] | |
testActorRef ! "World" | |
//expecting a message --> taking the first message off the mailbox | |
val answer = expectMsgType[String](10.milliseconds) | |
assert(answer == "World") | |
} | |
} | |
class MyActorWithState extends Actor { | |
var state: String = _ | |
override def receive: Receive = { | |
case x: String => | |
state = x | |
sender() ! state | |
} | |
} | |
/* | |
Test-Output | |
[info] StatefulnessBetweenTestsWithImplicitSenderSpec: | |
[info] - 1st test | |
[info] - 2nd test *** FAILED *** | |
[info] "[Hello]" did not equal "[World]" (StatefulnessBetweenTestsWithImplicitSenderSpec.scala:30) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment