Although Akka.NET has built-in support for testing, it is sometimes hard to find out what’s really going on inside your actor system.
A useful feature that can help you solve this problem during integration testing is the EventFilter
. The EventFilter
is a tool to verify that actors write certain log messages. It can also detect if messages go to DeadLetters and if certain exceptions are thrown
.
2 examples:
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
[Test] | |
public void MailActor_Should_Log_MailSent() | |
{ | |
var mailActor = ActorOf(Props.Create<MailActor>()); | |
var options = CreateOptions(); | |
var mail = CreateMail(); | |
var message = new SendMailMessage(mail, options); | |
EventFilter.Info("mail sent").ExpectOne(() => | |
{ | |
mailActor.Tell(message); | |
}); | |
} |
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
[Test] | |
public void MailActor_Should_Throw_Exception_When_SMTPConfiguration_Is_Missing() | |
{ | |
var mailActor = ActorOf(Props.Create<MailActor>()); | |
var options = CreateOptions(); | |
var mail = CreateMail(); | |
var message = new SendMailMessage(mail, options); | |
EventFilter.Exception<SMTPException>() | |
.ExpectOne(() => mailActor.Tell(message)); | |
} |
Remark: By default the event filter searches for exact matches.