【问题标题】:How to unit test stateless service in service fabric如何在服务结构中对无状态服务进行单元测试
【发布时间】:2018-03-17 01:37:11
【问题描述】:

我想在我的单元测试中创建一个继承类 StatelessService 的类 A 的实例。但我不能。我什么都试过了:模拟依赖,实现我自己的上下文等等。

当我尝试创建实例时,StatelessService 在内部某处抛出 NullReferenceException。

能做到吗?

class A : StatelessService
{
    public A(StatelessServiceContext context) : base(context /* Here will be thrown NullReferenceException */)
    {
        // It will never even get there.
    }
}

class UnitTest
{
    public void TestMethod()
    {
        var activationContext = MOCK<ICodePackageActivationContext>();
        var context = new StatelessServiceContext(..., activationContext, ...);
        var a = new A(context); // Here will be thrown an exception.
    }
}

【问题讨论】:

  • 提供一些代码作为minimal reproducible example,以更好地展示您的问题。它可用于帮助识别问题并得出任何可能的解决方案。
  • @Nkosi 我更新了提供最少伪代码的问题。
  • 您还必须更详细地说明何时何地出现错误。看看这里的示例,看看你是否能找到一个有帮助的例子github.com/Azure-Samples/service-fabric-watchdog-service/blob/…
  • 如果您尝试创建无状态服务的实例,@Nkosi 服务结构会在内部引发 NullReferenceException。描述什么?

标签: unit-testing azure-service-fabric service-fabric-stateless


【解决方案1】:

这是可以做到的。但与其重新发明轮子,不如看看 service fabric mocks https://github.com/loekd/ServiceFabric.Mocks

它包含适合您的场景类型的有用助手。

【讨论】:

  • 我试过使用它,它不起作用,并且在不同版本的服务结构上抛出各种异常。无论如何,我已经解决了我的问题。它带有类型初始化器和一些未在 my 类中设置的静态属性。这与服务结构无关。
  • @EwanCoder,请考虑将反馈/问题发布到包中,这样每个人都可以受益。 github.com/loekd/ServiceFabric.Mocks/issues
猜你喜欢
  • 2010-09-07
  • 1970-01-01
  • 2023-03-16
  • 2020-07-10
  • 2017-05-23
  • 1970-01-01
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多