我有一个看起来像这样的 Actor :
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;
}
}
我想知道是否有办法测试这种类型的交互。我们使用 ServiceFabric.Mocks库来设置在正在运行的服务之外创建参与者所需的所有脚手架,但当它到达 ActorProxy.Create<ICustomerActor>(customerId.Value)
时行,它会抛出异常,因为没有任何东西像真正的服务那样真正捆绑在一起。
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)
最佳答案
将 ActorProxyFactory 注入(inject)到调用 Actor 构造函数中,并使用它来创建 ActoryProxy 实例。如图所示here或here .
然后使用模拟库创建模拟代理工厂。像这个:https://github.com/loekd/ServiceFabric.Mocks
关于c# - 如何对在其方法内获取另一个参与者的引用的参与者进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40666422/