0

I have a test for particular actor. This actor depends on some other actors, so I use TestProbe() to test in isolation. My problem is, that I receive more messages then I am interested in testing at this very particular test. For example:

val a = TestProbe()
val b = TestProbe()
val actor = TestActorRef(new MyActor(a.ref, b.ref))

actor ! Message(1, 2)

b.expectMsg(3)

The test fails, because while creating MyActor it sends some kind of "registration" message to ones passed in constructor.

The message 3 arrives eventually, but assertion fails - this is not the first message to arrive. I would like to avoid asserting more messages than I need for a test - those can change, etc, it is not the scope of a particular test anyway.

As the TestProbe does not contain such methods - I suspect there may be something wrong with my test setup (or rather my project architecture then). I see there are many methods like fishForMessage but all those require a explicit time parameters which seems like irrelevant as my whole test is purely synchronous.

Is there any way to accomplish such test is desired message is just among all the were received? If not, how can my setup can be improved to be easy testable?

M4ks
  • 11,744
  • 7
  • 27
  • 48

1 Answers1

0

The fishForMessage actually fits. All these assertions including expectMsg are asynchronous. expectMsg just uses the preconfigured timeFactor as timeout.

TestActorRef guarantees you only that CallingThreadDispatcher will be used to send messages and execute futures (if they use dispatcher from the test actor), so they will act sequentially til they're use context.dispatcher. Nothing stops some code inside your MyActor from using another dispatcher to send a response, so all checks still should be asynchronous - you just can't get rid of that.

Community
  • 1
  • 1
dk14
  • 22,206
  • 4
  • 51
  • 88