编写Service Fabric应用程序类的UnitTest [英] Writing UnitTests for a Service Fabric Application Class

查看:51
本文介绍了编写Service Fabric应用程序类的UnitTest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Service Fabric应用程序类编写单元测试.我遇到了一些我不知道如何解决的错误.类定义的种类如下:

I am writing unit tests for a Service Fabric Application class. I am running into some errors which I don't understand how to fix. The class definition is of the sort:

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:

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

我收到以下错误:

没有给出与所需形式相对应的参数的参数上下文""SearchServiceClass.SearchServiceClass(StatelessServiceContext)".

There is no argument given that corresponds to the required formal parameter 'context' of 'SearchServiceClass.SearchServiceClass(StatelessServiceContext)'.

有人可以告诉我如何解决此问题吗?

Could someone please tell me how to fix this?

我一直在查看ServiceFabric.Mocks.我了解的是,我需要使用 MockStatelessServiceContextFactory.Default 创建模拟上下文.以下是正确的方法,我该怎么做?:

I have been looking at ServiceFabric.Mocks. What I understand is that I need to use the MockStatelessServiceContextFactory.Default to create a mock context. How do I do this, is the following the right way?:

var searchServiceClass = new SearchServiceClass(MockStatelessServiceContextFactory.Default);

推荐答案

是的,您可以使用 ServiceFabric.Mocks 库,以使用以下代码创建服务的测试实例:

Yes, you can use the ServiceFabric.Mocks library, to create a test instance of your service by using the following code:

var serviceInstance = new SearchServiceClass(MockStatelessServiceContextFactory.Default);

作为 Default 上下文的替代方法,您还可以构建自定义实例:

As an alternative to the Default context, you can also build a customized instance:

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.

这篇关于编写Service Fabric应用程序类的UnitTest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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