How to test DistributedPubSub with the TestKit in Akka.net?

653 views Asked by At

I am unit-testing an actor that uses the Cluster tool DistributedPubSub. For the tests I am using the TestKit of Akka.net.

Apparently, in the TestKit, the system actor Sys doesn't have the DistributedPubSub tool and it throws a Null pointer exception when accessing it.

This is the code failing when the actor is created as child of Sys:

var mediador = DistributedPubSub.Get(Context.System).Mediator;

Is there a way to mock it or create an actor probe to use it with Sys?

1

There are 1 answers

0
Aaronontheweb On BEST ANSWER

I think the issue is here that you need to specify the following in your HOCON when you pass it to the base class constructor, when working with any of the TestKit classes:

public class DistributedPubSubMediatorSpec : TestKit
{
    public DistributedPubSubMediatorSpec() : base(GetConfig()) { }

    public static Config GetConfig()
    {
        return ConfigurationFactory.ParseString("akka.actor.provider = \"Akka.Cluster.ClusterActorRefProvider, Akka.Cluster\"");
    } 
}

I added a pull request to test for this issue on Akka.NET here and the spec passes: https://github.com/akkadotnet/akka.net/pull/2424