Akka.net and Unit tests

一世执手 提交于 2019-12-05 19:09:52

ExpectMsg works only when combined with special kind of an actor called TestActor. You can access/create it from test kit. It's role is to catch and verify messages send to it.

If you redesign your SubscriptionService actor a little, you could pass test actor refs to it. The easiest way to do so is to simply inject actor refs through actor constructor - I've used ICanTell interface, which is the more general form, implemented by both actor refs and actor selection:

public class SubscriptionService : ReceiveActor
{
    private readonly ICanTell service1;
    private readonly ICanTell service2;

    public SubscriptionService(ICanTell service1, ICanTell service2)
    {
        this.service1 = service1;
        this.service2 = service2;
        this.Receive<RequestMessage>(message => this.ProcessRequestMessage(message));
    }

This way you may create your actor using:

Context.ActorOf(Props.Create(() => new SubscriptionService(Context.ActorSelection("Service1"), Context.ActorSelection("Service2")));

To test it, in your TestKit spec class initialize it using either TestActor or TestProbe.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!