Make a TestProbe to forget all the messgaes

69 Views Asked by At

I am writing a code in a test driven way, and after having written many tests, I introduced a new actor, and in my test environment I use a TestProbe for it. Now in the n-th test, I am testing that the new actor receives a message. But the message he actually receives is not the expected one, because he receives many message from other tests, before the expected one. So is there a way to tell the TestProbe to forget all the messages received until now, without specifying the number, and start listening the messages from now?

Some code:

class MyTest  extends TestKit(ActorSystem("test")) {

  var myActorRef: ActorRef = _
  var myTestProbe = TestProbe()

  before {
    myActorRef = system.actorOf(MyActor.props(myTestProbe.ref))
  }

  "MyActor" must {

    //.... many tests here

    "trigger notification to myTestProbe" in {
        // here I would like myTestProbe to forget all the messages he received before
          myActorRef ! AMessageThatShoudCauseAnotherMessageToBeSentToMyProbe(..)


      myProbe.expectMsg(
        TheExpectedMessage(...) //this fails, because the message received has been sent before this test.
      )
    }

I can do myTestProbe.receiveN(200) but I need the exact number of messages, and if I then add another test before, this number could change... it's really bad.

I could also to re-create the testProbe and the actor under test:

myTestProbe = TestProbe()
myActorRef = system.actorOf(MyActor.props(myTestProbe.ref))

but for other reasons, I would prefer to avoid it... any other suggestion?

0

There are 0 best solutions below