mardi 9 juin 2015

How to use TestKit in Akka.NET

I'm trying to test my Akka.NET actors, but are having some trouble with the TestKit and understanding how it works.

Since there is no official documentation for unit testing in Akka.NET yet, I've explored the Akka.NET repo for example code, but the examples used there doesn't work for me.

The tests I've used for reference is ReceiveActorTests.cs and ReceiveActorTests_Become.cs, since those are close to the scenario I'm trying to test in my app.

Here's some dummy code:

Given this actor

public class Greeter : ReceiveActor
{
    public Greeter()
    {
        NotGreeted();
    }

    private void NotGreeted()
    {
        Receive<Greeting>(msg => Handle(msg));
    }

    private void Greeted()
    {
        Receive<Farewell>(msg => Handle(msg));
    }

    private void Handle(Greeting msg)
    {
        if (msg.Message == "hello")
        {
            Become(Greeted);
        }
    }

    private void Handle(Farewell msg)
    {
        if (msg.Message == "bye bye")
        {
            Become(NotGreeted);
        }
    }
}

I want to test that it Receives the Greeting and Farewell messages correctly, and enters the Become-states correctly. Looking at the ReceiveActorTests_Become.cs tests, an actor is created by

var system = ActorSystem.Create("test");
var actor = system.ActorOf<BecomeActor>("become");

and a message is sent and asserted by

actor.Tell(message, TestActor);
ExpectMsg(message);

However, when I try this approach to instantiating an actor, and many others based on TestKit methods (see below), I keep getting the samme failed test error:

Xunit.Sdk.TrueExceptionFailed: Timeout 00:00:03 while waiting for a message of type ConsoleApplication1.Greeting 
Expected: True
Actual:   False

This is my test:

public class XUnit_GreeterTests : TestKit
{
    [Fact]
    public void BecomesGreeted()
    {
        //var system = ActorSystem.Create("test-system"); // Timeout error
        //var actor = system.ActorOf<Greeter>("greeter"); // Timeout error
        //var actor = ActorOfAsTestActorRef<Greeter>("greeter"); // Timeout error
        //var actor = ActorOf(() => new Greeter(), "greeter"); // Timeout error
        //var actor = Sys.ActorOf<Greeter>("greeter"); // Timeout error
        //var actor = Sys.ActorOf(Props.Create<Greeter>(), "greeter"); // Timeout error
        var actor = CreateTestActor("greeter"); // Works, but doesn't test my Greeter actor, but rather creates a generic TestActor (as I understand it)

        var message = new Greeting("hello");

        actor.Tell(message, TestActor);

        ExpectMsg(message);
    }
}

I've also tried moving the ExpectMsg line above the actor.Tell line (since it made more sense to Expect something before you act on it and rather verify the expectation after), but this also results in the Timeout error.

I've tried with both NUnit and XUnit TestKits.

There's probably something really basic I've overlooked.

Aucun commentaire:

Enregistrer un commentaire