服务结构单元测试和依赖注入 [英] Service Fabric Unit Testing and Dependency Injection

查看:99
本文介绍了服务结构单元测试和依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能仅通过调用Reliable Service/Actor的构造函数然后对其进行测试来对其进行测试. var testService = new SomeService();引发NullReferenceException.那么我该如何处理已部署的服务.

I can't test a Reliable Service/Actor by just calling it's constructor and then test it's methods. var testService = new SomeService(); throws a NullReferenceException. So what can I do with deployed Service..

我了解部署的SF Reliable Services/Actor不是标准的.NET类,并且对部署的S/A进行单元测试可能是一个奇怪的主意.

I understand that deployed SF Reliable Services/Actors are not standard .NET classes, and unit testing of deployed S/A maybe a strange idea.

无论如何,现在我想尝试一下.

Anyway now I'm trying to give it a try.

例如.我刚刚部署了Service,然后在测试中创建了Proxy对象,并将项目添加到Service的输入队列中.然后,我需要断言输入队列计数=1.如果我刚刚部署了Service且没有其他客户端/服务/Actor使用过它的输入队列,那么它可以工作.但是下一次该测试将失败,这就是问题所在.我需要使该Service停止与其他使用者的操作,放弃它的队列并进行测试.为此,我可以创建一些TestMode属性和某些方法(如PropareoForTests/TestingCompleted),然后在测试之前和之后从测试客户端调用它们.

For example. I've just deployed a Service, than in the test I've created a Proxy object and added item into input queue of Service. Then I need to assert that input queue count = 1. And it works if I've just deployed a Service and no other Clients/Services/Actors have used it's input queue. But next time this test will be failed that's the problem. I need make the Service to stop operatating with other consumers, drop it's queue and than test it. For this purpose I can create some TestMode property and some methods like PropareoForTests/TestingCompleted and call them from test client before and after testing.

那样做是一个坏主意吗?也许有一些关于SF单元测试的指南?谢谢.

Is this is a bad idea to do it like that. Maybe are there some guidelines for unit testing SF? Thanks.

更新:

在调查 Service Fabric Web参考应用程序示例时,我发现了这一点TODO字符串:

While investigating Service Fabric Web Reference Application example I've found this TODO string:

/// TODO: Temporary property-injection for an IServiceProxyWrapper until constructor injection is available.

这是否意味着SF Services将改善对DI的支持?演员呢?

Does it mean that SF Services will improve it's DI support? What about actors?

推荐答案

实际上,您可以以与测试.NET中任何其他类相同的方式来测试Reliable Services和Actors!它们的特殊之处在于它们在底层平台中使用了某些挂钩,但除此之外,您可以正常实例化服务或actor类并在其上调用方法.

Actually you can test Reliable Services and Actors the same way you'd test any other class in .NET! They're only special in that they use certain hooks into the underlying platform, but other than that you can instantiate your service or actor class normally and call methods on it.

当前,可靠服务更容易进行单元测试,因为进入平台的主要钩子即状态管理器是可通过构造函数插入的接口.

Currently, Reliable Services are a little easier to unit test because the primary hook into the platform, the State Manager, is an interface that's pluggable through the constructor.

例如,您的服务类可能如下所示:

For example, your service class might look like this:

已使用GA版本API(2.0.135)

Updated with the GA release API (2.0.135)

class MyService : StatefulService
{
  public MyService (StatefulServiceContext context, IReliableStateManager stateManager)
      :base (context, stateManager)
  {
  }

  public void MyMethod()
  {
    // do stuff..
  }
}

然后您可以像这样测试服务等级:

Then you can test your service class like so:

[TestMethod]
public TestMyMethod()
{
  MockReliableStateManager stateManager = new MockReliableStateManager();
  MyService target = new MyService(stateManager);

  target.MyMethod();
  // validate results and all that good stuff
}

我们有一个完整的实际服务示例,其中有大量依赖项已在GitHub上进行了单元测试: https://github.com/Azure-Samples/service-fabric-dotnet-management-party-cluster

We have a full working example of actual services with lots of dependencies being unit tested available on GitHub: https://github.com/Azure-Samples/service-fabric-dotnet-management-party-cluster

此示例还具有IReliableStateManager和IReliableDictionary模拟,您可以将其用作自己的单元测试的起点.

This example has IReliableStateManager and IReliableDictionary mocks as well that you can use as a starting point for your own unit tests.

这篇关于服务结构单元测试和依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆