Akka.Net 测试使用 TestKit 创建/监督子actor

And*_*nde 1 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经让这个测试工作了,但我显然错过了一些东西,因为我根本没有使用太多的 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);
    }
Run Code Online (Sandbox Code Playgroud)

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

And*_*ewS 5

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

测试父/子关系更为复杂。这是 Akka.NET 承诺提供简单抽象使测试变得更加困难的一种情况。

测试这种关系的最简单方法是使用消息传递。例如,您可以创建一个父actor,一旦它启动,它的子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!");
    }
}
Run Code Online (Sandbox Code Playgroud)

测试亲子关系的注意事项

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

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