0

I am testing with NUnit. What am I doing wrong?

public class ZipActor : ReceiveActor
{
    public ZipActor()
    {
        Receive<ZipMessage>(message => HandleZipMessage(message));
    }

    private void HandleZipMessage(ZipMessage message)
    {
        Console.WriteLine(string.Format("Received: {0} for {1}", typeof(ZipMessage).Name, message.SourceFolderPath));
    }
}

public class ZipMessage
{
    public readonly string SourceFolderPath;

    public ZipMessage(string sourceFolderPath)
    {
        SourceFolderPath = sourceFolderPath;
    }
}

[TestFixture]
public class ZipActorTests : TestKit
{
    [Test]
    public void ZipActor_WhenSentZipMessage_ShouldReceiveZipMessage()
    {
        var actor = Sys.ActorOf(Props.Create(() => new ZipActor()));
        string path = "some path";

        actor.Tell(new ZipMessage(path));

        ExpectMsg<ZipMessage>();
    }
}

I am getting this error:

Failed: Timeout 00:00:03 while waiting for a message of type CloudBackupActors.Messages.ZipMessage

This is what I get in NUnit'sconsole output. Received: ZipMessage for some path [WARNING][16/11/2015 18:46:37][Thread 0012][akka://test/user] DeadLetter from [akka://test/user] to [akka://test/user]: : [akka://test/user/$b], ExistenceConfirmed=True, AddressTerminated=False>

2 Answers2

2

You are misunderstanding which actor is the subject of ExpectMsg(). The ExpectMsg call is being called on the TestActor, which is the default/implicit sender of all messages in TestKit tests.

So the error is because you are telling the TestKit that the TestActor should expect a ZipMessage. To be clear, as written, is saying that the TestActor should get a ZipMessage. It is not saying that the ZipActor should get that message. The timeout you are hitting is the default :03 timeout that the TestActor applies to its ExpectMsg call.

To make the test pass as it's set up now, you'd need to Sender.Tell(new ZipMessage("foopath")) from inside the ZipActor.

In all fairness to you, the project docs for the TestKit are currently missing (as of 11/16/15). We're in the middle of writing them now and they should be published next week.

For now I suggest you check out this thorough intro to the TestKit (disclosure: I wrote it). I think it will give you a very solid understanding of how the TestKit actually works.

AndrewS
  • 1,395
  • 11
  • 23
0

OK, so I got the wrong end of the stick here and it makes sense when I actually add the end line of HandleZipMessage that I had missed out. My test is in fact analogous to this post - Testing Akka.NET's Context.Parent with TestKit. So what I needed to do is this, which works. :)

public class ZipActor : ReceiveActor
{
    public ZipActor()
    {
        Receive<ZipMessage>(message => HandleZipMessage(message));
    }

    private void HandleZipMessage(ZipMessage message)
    {
        Console.WriteLine(string.Format("Received: {0} for {1}", typeof(ZipMessage).Name, message.SourceFolderPath));
        // TODO: Zip operations
        Context.Parent.Tell(new IncrementFolderCountMessage());
    }
}

public class ZipMessage
{
    public readonly string SourceFolderPath;

    public ZipMessage(string sourceFolderPath)
    {
        SourceFolderPath = sourceFolderPath;
    }
}

[TestFixture]
public class ZipActorTests : TestKit
{
    [Test]
    public void ZipActor_WhenReceivedZip_ShouldIncrementFolderCount()
    {
        // Arrange
        // (make ZipActor child of TestActor)
        var props = Props.Create(() => new ZipActor());
        var actor = ActorOfAsTestActorRef<ZipActor>(props, TestActor);

        string path = "some path";

        // Act
        actor.Tell(new ZipMessage(path));

        // Assert
        ExpectMsg<IncrementFolderCountMessage>();
    }
}
Community
  • 1
  • 1