Akka.Net测试用TestKit创建/监督儿童演员

时间:2015-10-23 10:47:07

标签: c# akka.net

我有类似以下的Akka.Net代码,我正在尝试为它编写测试:

public class DoesSomethingActor : UntypedActor
{
    protected override void OnReceive(object message)
    {

    }
}

public class ForwardsMessagesActor : UntypedActor
{
    protected override void OnReceive(object message)
    {
        var actor = Context.ActorOf(Context.DI().Props<DoesSomethingActor>(), "DoesSomethingWorker");

        for (int i = 0; i < 5; i++)
        {
            actor.Tell(message + " " + i);
        }
    }
}

我已经完成了这项测试,但我显然遗漏了一些东西,因为我根本没有使用太多的TestKit。是否仍然没有关于如何使用TestKit进行测试的官方文档?

//creating actor mocks with Moq seems to confuse Akka - it just doesn't work 
//but creating mock classes manually like this, 
//then configuring them in the DI container works
public class DoesSomethingActorSpy : DoesSomethingActor
{
    public static List<object> ReceivedMessages = new List<object>();

    protected override void OnReceive(object message)
    {
        ReceivedMessages.Add(message);
    }
}

    [TestMethod]
    public void ForwardsMessagesActor_Creates5Messages()
    {
        //set up DI container to use DoesSomethingActorSpy as a child actor
        ContainerBuilder builder = new ContainerBuilder();
        builder.RegisterType<ForwardsMessagesActor>();
        builder.RegisterType<DoesSomethingActorSpy>().As<DoesSomethingActor>();
        IContainer container = builder.Build();

        var propsResolver = new AutoFacDependencyResolver(container, Sys);

        var actor = ActorOfAsTestActorRef<ForwardsMessagesActor>(propsResolver.Create<ForwardsMessagesActor>());

        actor.Tell("Test");

        //this looks wrong, I probably should be using something from TestKit
        Thread.Sleep(10);

        CollectionAssert.AreEquivalent(
            new[] { "Test 0", "Test 1", "Test 2", "Test 3", "Test 4" },
            DoesSomethingActorSpy.ReceivedMessages);
    }

我应该如何创建模拟演员? TestKit上是否有任何方法可以调用等待所有消息都被处理完毕?

1 个答案:

答案 0 :(得分:4)

引自How to Test Akka.NET Actors: Unit Testing w/ Akka.TestKit

  

测试父/子关系更复杂。在这种情况下,Akka.NET提供简单抽象的承诺使测试更加困难。

     

测试这种关系的最简单方法是使用消息传递。例如,您可以创建一个父actor,其子项在启动时会向另一个actor发送消息。或者您可以让父母向孩子转发消息,然后孩子可以回复原始发件人,例如:

public class ChildActor : ReceiveActor
{
    public ChildActor()
    {
        ReceiveAny(o => Sender.Tell("hello!"));
    }
}

public class ParentActor : ReceiveActor
{
    public ParentActor()
    {
        var child = Context.ActorOf(Props.Create(() => new ChildActor()));
        ReceiveAny(o => child.Forward(o));
    }
}

[TestFixture]
public class ParentGreeterSpecs : TestKit
{
    [Test]
    public void Parent_should_create_child()
    {
        // verify child has been created by sending parent a message
        // that is forwarded to child, and which child replies to sender with
        var parentProps = Props.Create(() => new ParentActor());
        var parent = ActorOfAsTestActorRef<ParentActor>(parentProps, TestActor);
        parent.Tell("this should be forwarded to the child");
        ExpectMsg("hello!");
    }
}
     

关于测试父/子关系的谨慎信息

     

避免将代码过度耦合到层次结构中!

     

过度测试父/子关系可以将您的测试耦合到您的层次结构实现。这会增加以后通过强制执行许多测试重写来重构代码的成本。您需要在验证意图和测试实施之间取得平衡。