3

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?

Alberto Monteiro
  • 5,989
  • 2
  • 28
  • 40

1 Answers1

3

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>();
    }
Usein Mambediiev
  • 243
  • 2
  • 11
  • @usein-mambediev I have this test in my code, I would like to know how to test properly if some actor sent a message to himself. Sometime I can have some code that I dont have another actor to use testprobe and expectmsg from that. Thanks for your comment Atm I solved my question using another actor instead to send message to *myself* – Alberto Monteiro Oct 20 '15 at 20:14
  • @AlbertoMonteiro, I believe that currently, there's no possibility to check such messages. The only possible way is to check messages sent from actor under test to other actors (test probes) or to verify that dependencies were called (using mocks). – Usein Mambediiev Oct 21 '15 at 17:10
  • @usein-mambediev Yeah, I got it, I am actualy doing this, and when I verify if dependencies were called, I use ````ExpectNoMsg```` to wait the actor execution and check if dependecie method were called. Thanks for yor help – Alberto Monteiro Oct 22 '15 at 22:08