Alberto Monteiro
Alberto Monteiro

Reputation: 6219

Testing self message send in Akka.NET

I am new in Akka.NET, at moment I am having difficulty in test if my actor sent any message to himself.

This is my actor code:

public class MySuperActor : ReceiveActor
{
    private readonly IActorRef _anotherActor;

    public RoteadorDeContratosSuspensoActor(IActorRef anotherActor)
    {
        _anotherActor = anotherActor;
        Receive<MySuperActorMessage>(m => HandleMessage(m));
        Receive<MySuperActorSuperMessage>(m => HandleSuperMessage(m));
    }

    private void HandleMessage(MySuperActorMessage message)
    {
        Self.Tell(new MySuperActorSuperMessage(message));
    }

    private void HandleSuperMessage(MySuperActorSuperMessage message)
    {
        _anotherActor.Tell(new AnotherActorMessage(message));
    }
}

And that is my test code

[TestFixture]
public class MySuperActorTest : TestKit
{
    private IActorRef _sut;
    private IActorFactory _actorFactory;
    private List<Contrato> _contratos;
    private Props _props;

    [Test]
    public void WhenReceiveASimpleMessageActorShouldSendSuperMessageToHimself()
    {
        var testProbe = CreateTestProbe();
        Props props = Props.Create<MySuperActor>(testProbe);
        var sut = ActorOf(_props);
        sut.Tell(new MySuperActorMessage());

        ExpectMsg<MySuperActorSuperMessage>();
    }
}

My test always break with the following message:

Failed: Timeout 00:00:03 while waiting for a message of type AkkaNetTestPlaygroung.MySuperActorSuperMessage

How can I check if my actor is sending another message to himself?

Upvotes: 3

Views: 2337

Answers (1)

Usein Mambediiev
Usein Mambediiev

Reputation: 243

Alberto.

Actually, calling ExpectMsg() in your test method would expect a message to be sent back to your test actor system (I mean a context, on which initial MySuperActorMessage was sent), but not to MySuperActor instance. It would be better (and more correct) to expect AnotherActorMessage instance on your test probe, that you create in your test.

Here is a test method that pass:

    [Test]
    public void WhenReceiveASimpleMessageActorShouldSendSuperMessageToHimself()
    {
        var testProbe = this.CreateTestProbe();
        var props = Props.Create<MySuperActor>(testProbe);
        var sut = this.Sys.ActorOf(props);
        sut.Tell(new MySuperActorMessage());

        testProbe.ExpectMsg<AnotherActorMessage>();
    }

Upvotes: 3

Related Questions