簡體   English   中英

Akka.Net使用TestKit測試兒童演員的創建/監督

[英]Akka.Net test Child actor creation/supervision with TestKit

我有與以下類似的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上的任何方法來等待所有消息處理完畢嗎?

引自“ 如何測試Akka.NET Actor:帶有Akka.TestKit單元測試”

測試父子關系更加復雜。 在這種情況下,Akka.NET提供簡單抽象的承諾使測試變得更加困難。

測試這種關系的最簡單方法是使用消息傳遞。 例如,您可以創建一個父角色,該角色的子角色一旦啟動,其子對象便會向另一個角色發出消息。 或者您可以讓父母將消息轉發給孩子,然后孩子可以回復原始發件人,例如:

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

關於測試父母/孩子關系的警告

避免將代碼過度耦合到層次結構!

過度測試父子關系會將您的測試耦合到層次結構實現中。 通過強制執行許多測試重寫,這將增加以后重構代碼的成本。 您需要在驗證意圖和測試實現之間取得平衡。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM