moswald
moswald

Reputation: 11677

How to unit-test an actor that gets a reference to another actor inside its method?

I've got an actor that looks something like this:

public class MyActor : Actor, IMyActor
{
    public async Task<ICustomerActor> GetCustomer()
    {
        var customerId = await StateManager.TryGetStateAsync<ActorId>(CustomerIdState);

        return customerId.HasValue
            ? ActorProxy.Create<ICustomerActor>(customerId.Value)
            : null;
    }
}

I want to know if there's a way to test this type of interaction. We use the ServiceFabric.Mocks library to set up all the scaffolding needed to create actors outside of a running service, but when it reaches the ActorProxy.Create<ICustomerActor>(customerId.Value) line, it throws an exception because nothing is really tied together like a real service would be.

System.ArgumentException
Failed to determine the current application, please provide application name.
Parameter name: applicationName
   at Microsoft.ServiceFabric.Actors.Generator.ActorNameFormat.GetFabricServiceUri(Type actorInterfaceType, String applicationName, String serviceName)
   at Microsoft.ServiceFabric.Actors.Client.ActorProxyFactory.CreateActorProxy[TActorInterface](ActorId actorId, String applicationName, String serviceName, String listenerName)
   at Microsoft.ServiceFabric.Actors.Client.ActorProxy.Create[TActorInterface](ActorId actorId, String applicationName, String serviceName, String listenerName)

Upvotes: 2

Views: 857

Answers (1)

LoekD
LoekD

Reputation: 11470

Inject an ActorProxyFactory into the calling Actor constructor, and use that to create ActoryProxy instances. Like shown here or here.

Then use a mock library to create mock proxyfactories. Like this one: https://github.com/loekd/ServiceFabric.Mocks

Upvotes: 3

Related Questions