问题
I would like to use Akka.net TestKit for writing Unit tests, but I have a question. I have a class SubscriptionService which is responsible for transmitting messages to the selected actors.
public class SubscriptionService : ReceiveActor
{
private readonly ActorSelection service1;
private readonly ActorSelection service2;
public SubscriptionService()
{
this.service1 = Context.ActorSelection("Service1");
this.service2 = Context.ActorSelection("Service2");
this.Receive<RequestMessage>(message => this.ProcessRequestMessage(message));
}
private void ProcessRequestMessage(RequestMessage message)
{
this.service1.Tell(message);
this.service2.Tell(message);
}
How can I test this behavior? I created this test but in this test I get the exception. "Additional information: Assert.Fail failed. Failed: Timeout 00:00:03 while waiting for a message"
[TestClass]
public class SubscriptionServiceUnitTests : TestKit
{
[TestMethod]
public void Test1()
{
var subscriptionServiceRef = this.ActorOfAsTestActorRef<SubscriptionService>("SubscriptionService");
subscriptionServiceRef.Tell(new RequestMessage());
var answer = this.ExpectMsg<RequestMessage>();
}
I mean How can I get message for service1 and service2 in Test1 method ?
回答1:
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.
来源:https://stackoverflow.com/questions/31103148/akka-net-and-unit-tests