0

Following is the actor, I've defined (trying to get my head around persistent actor!!)

public class Country : ReceivePersistentActor
{
    public override string PersistenceId => GetType().Name + state.Id;

    private CountryState state;

    public Country()
    {
        Command<CreateCountry>(CreateCountry);
    }

    private bool CreateCountry(CreateCountry cmd)
    {
        Persist(new CountryCeated
        {
            Id = cmd.Id,
            Code = cmd.Code,
            Description = cmd.Description,
            Active = cmd.Active
        }, evt =>
        {
            state = new CountryState
            {
                Id = evt.Id,
                Code = evt.Code,
                Description = evt.Description,
                Active = evt.Active
            };
        });

        return true;
    }
}

Following is unit test case that I've defined:

[TestClass]
public class CountrySpec : TestKit
{
    [TestMethod]
    public void CountryActor_Should_Create_A_Country()
    {
        var country = Sys.ActorOf(Props.Create(() => new Country()), "Country");
        country.Tell(new CreateCountry(Guid.NewGuid(), "UK", "United Kingdom", true));
        ExpectNoMsg();
    }
}

When I run the test case, there is an exception that I can see in the output window of the test case

[ERROR][25/08/2016 08:25:07][Thread 0007][akka://test/user/Country] Object reference not set to an instance of an object.
Cause: [akka://test/user/Country#552449332]: Akka.Actor.ActorInitializationException: Exception during creation ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Domain.Country.get_PersistenceId() in Y:\Spikes\StatefulActors\Domain\Country.cs:line 9
   at Akka.Persistence.Eventsourced.StartRecovery(Recovery recovery)
   at Akka.Persistence.Eventsourced.AroundPreStart()
   at Akka.Actor.ActorCell.<>c__DisplayClass154_0.<Create>b__0()
   at Akka.Actor.ActorCell.UseThreadContext(Action action)
   at Akka.Actor.ActorCell.Create(Exception failure)
   --- End of inner exception stack trace ---
   at Akka.Actor.ActorCell.Create(Exception failure)
   at Akka.Actor.ActorCell.SysMsgInvokeAll(EarliestFirstSystemMessageList messages, Int32 currentState)

but the test case is marked as success enter image description here

Is there any way/settings in the TestKit, where it can be set such that for any exception, mark the test case failed?

4

1 回答 1

1

默认情况下,actor 内部的任何异常都被封装——这意味着它们不会冒泡,从而破坏系统的其余部分。

参与者进入系统,可以通过观察他们相互通信的方式来进行测试。通常它涉及从参与者系统提供输入和断言输出 - 在您的案例中测试已经通过,因为您没有验证任何输出。从你的测试的角度来看,这个演员可能已经死了,而且不会有什么不同。

验证输出(通过参与者自身内部的断言或即使用自定义测试日志)是使用测试的最佳方式。

如果由于某种原因您仍然需要在参与者内部捕获异常,您可以创建绑定到 ie 的监督策略TestActor,所有异常都可以被转发:

public class TestingStrategy : OneForOneStrategy
{
    protected TestingStrategy(IActorRef probe) : base(exception =>
    {
        probe.Tell(exception);
        return DefaultDecider.Decide(exception);
    }) { }
}
于 2016-08-25T11:17:35.983 回答