简体   繁体   English

为 Service Fabric 应用程序编写单元测试 Class

[英]Writing UnitTests for a Service Fabric Application Class

I am writing unit tests for a Service Fabric Application class.我正在为 Service Fabric 应用程序 class 编写单元测试。 I am running into some errors which I don't understand how to fix.我遇到了一些我不明白如何解决的错误。 The class definition is of the sort: class 定义是这样的:

namespace SearchService
{
    internal sealed class SearchServiceClass : StatelessService
    {
        //variables defined followed by constructor
        private string jsonStr;
        public SearchServiceClass(StatelessServiceContext context)
            : base(context)
        {
            //constructor stuff
        }
        
        public bool IsDataJsonLoaded
        {
            get
            {
                return !(jsonStr == null);
            }
        }
    }
}

The application has a test class defined as follows:该应用程序有一个测试 class 定义如下:

namespace SearchService.Tests
{
    //[TestClass]
    public class SearchServiceClassTest
    {
        [Fact]
        public void SearchServiceClassConstructor()
        {
           var searchServiceClass = new SearchServiceClass();
           Assert.True(searchServiceClass.IsDataJsonLoaded);
        }
    }
}

I get the following error:我收到以下错误:

There is no argument given that corresponds to the required formal parameter 'context' of 'SearchServiceClass.SearchServiceClass(StatelessServiceContext)'.没有给出与“SearchServiceClass.SearchServiceClass(StatelessServiceContext)”所需的形式参数“context”相对应的参数。

Could someone please tell me how to fix this?有人可以告诉我如何解决这个问题吗?

Edit: I have been looking at ServiceFabric.Mocks.编辑:我一直在看 ServiceFabric.Mocks。 What I understand is that I need to use the MockStatelessServiceContextFactory.Default to create a mock context.我的理解是我需要使用MockStatelessServiceContextFactory.Default来创建模拟上下文。 How do I do this, is the following the right way?:我该怎么做,以下是正确的方法吗?:

var searchServiceClass = new SearchServiceClass(MockStatelessServiceContextFactory.Default);

Yes, you can use the ServiceFabric.Mocks library, to create a test instance of your service by using the following code:是的,您可以使用ServiceFabric.Mocks库,通过以下代码创建服务的测试实例:

var serviceInstance = new SearchServiceClass(MockStatelessServiceContextFactory.Default);

As an alternative to the Default context, you can also build a customized instance:作为Default上下文的替代方案,您还可以构建自定义实例:

var newUri = new Uri("fabric:/MockApp/OtherMockStatelessService");
var serviceTypeName = "OtherMockServiceType";
var partitionId = Guid.NewGuid();
var replicaId = long.MaxValue;
var context = new MockCodePackageActivationContext("fabric:/MyApp", "MyAppType", "Code", "Ver", "Context", "Log", "Temp", "Work", "Man", "ManVer");
var context = MockStatelessServiceContextFactory.Create(context, serviceTypeName, newUri, partitionId, replicaId);
var serviceInstance = new SearchServiceClass(context);

See this sample test and this one for more information.有关更多信息,请参阅此示例测试测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM