【问题标题】:How to unit-test an actor that gets a reference to another actor inside its method?如何对在其方法中获取对另一个参与者的引用的参与者进行单元测试?
【发布时间】:2017-04-01 16:12:40
【问题描述】:

我有一个看起来像这样的演员:

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 库来设置在正在运行的服务之外创建actor 所需的所有脚手架,但是当它到达ActorProxy.Create&lt;ICustomerActor&gt;(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)

【问题讨论】:

    标签: c# unit-testing actor azure-service-fabric stateful-actor-service


    【解决方案1】:

    将 ActorProxyFactory 注入到调用的 Actor 构造函数中,并使用它来创建 ActoryProxy 实例。如herehere 所示。

    然后使用模拟库创建模拟代理工厂。喜欢这个:https://github.com/loekd/ServiceFabric.Mocks

    【讨论】:

    • 我想到了这一点,但希望有更好的解决方案。 :) 我只是希望有一种方法可以“修复”常规的 ActorProxy.Create 方法。
    • 要清楚,这不是一个坏方法,但所有文档都说“要获得演员参考,请致电ActorProxy.Create”。这将改变这一点,现在有两种方法可以做同样的事情。当库作者不考虑他们的静态和/或扩展方法如何影响需要编写测试的应用程序作者时,我总是很恼火。 ://
    • 我更喜欢这种方法,但也有来自 Fakes 框架的 Shims。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 2015-08-20
    相关资源
    最近更新 更多