How to Unit Test Akka.NET Actors with Akka.TestKit
An Introduction to the Akka.TestKit
In this post we introduce the Akka.TestKit module - a library that makes it easy to unit test Akka.NET actors using all of the popular .NET unit testing frameworks.
A Brief End-to-End Akka.TestKit
Example
Before going deep into the TestKit
, here’s an end-to-end example of what a test usually looks like.
[TestFixture] //using NUnit
public class UserIdentityActorSpecs : TestKit{
[Test]
public void UserIdentityActor_should_confirm_user_creation_success()
{
var identity = Sys.ActorOf(Props.Create(() => new UserIdentityActor()));
identity.Tell(new UserIdentityActor.CreateUserWithValidUserInfo());
var result = ExpectMsg<UserIdentityActor.OperationResult>().Successful;
Assert.True(result);
}
}
80% of your tests will be this simple: create an actor, send it a message, and expect a response back. Let’s explore how to do it.