在Akka.NET中测试自我消息发送

时间:2015-10-19 17:51:35

标签: c# unit-testing akka.net akka-testkit

我是Akka.NET的新手,如果我的演员向自己发送了任何信息,我的测试有困难。

这是我的演员代码:

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));
    }
}

那是我的测试代码

[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>();
    }
}

我的测试总是打破以下消息:

失败:在等待类型为AkkaNetTestPlaygroung.MySuperActorSuperMessage

的消息时超时00:00:03

如何检查我的演员是否正在向自己发送另一条消息?

1 个答案:

答案 0 :(得分:2)

阿尔贝托。

实际上,在测试方法中调用ExpectMsg()会将消息发送回测试actor系统(我的意思是发送初始MySuperActorMessage的上下文),而不是MySuperActor实例。期望在您的测试中创建的测试探针上的AnotherActorMessage实例会更好(也更正确)。

这是一个传递的测试方法:

    [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>();
    }