Using the TestKit
seems to be the recommended and 'official' practice. But you can only test the actor in its full behavior, as part of a 'fake' system (the test system).
I might be resisting that only because I am starting with actors, but that doesn't feel right to me. I want/need to unit test my actor's methods individually, not as part of a system.
So what I have been doing so far is taking away the logic part of the actor and the state in another class that I can unit test at will, and having an instance of that class in the real akka actor. You can even map and methods properties if you want to abstract that away.
I have seen this recommended here for akka on Scala: How to test a public method in an akka actor?, but it applies to akka.net.
While testing the actor as part of a system makes sense, my unit testing requirements conflict with that.
My example: I was creating an actor that received data from a network
call, and updated the UI if that data changed in specific ways. Now
the common approach to test the data processing with TestKit
would
need me to either make the actor answer the message with his new data
(not my intention at first), or make multiple calls for each change
scenario - and then data processing would be tested in conjunction
with state comparison. I don't like either changing the behavior just
to allow testing nor testing 2 or more different parts of the actor.
So I did as I explained, extracted a logic class that handles each message type in a different method. I can test data processing, I can change the state and test the state comparison methods. Much cleaner that way.
Finally, if you are playing around with akka, you do need to use TestKit
. You'll want to test the actor's full behavior. But I still need more granular testing, and wonder why it's not possible to new up actors to do just that (though I get it should NEVER be done out of testing context).