本文介绍了有关使用Mocks进行单元测试Service Fabric应用程序的特定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是针对以下问题的后续活动:为服务编写单元测试织物应用程序类

This question is a follow up to: Writing UnitTests for a Service Fabric Application Class

我的应用程序定义属于此类-简化了它以解决我面临的最基本问题:

My application definition is of the type - simplified it to solve the most basic of problems I am facing:

namespace SearchService
{
    internal sealed class SearchServiceClass : StatelessService
    {
        //variables defined followed by constructor
        private string jsonStr;
        public SearchServiceClass(StatelessServiceContext context)
            : base(context)
        {
            try
            {
                var dataPackage = Context.CodePackageActivationContext
                .GetDataPackageObject("Data");
                jsonStr = File.ReadAllText(dataPackage.Path + @"\data.json");
            }
            catch
            {
                //exception handling code
                throw;
            }
        }
        
        public bool IsDataJsonLoaded
        {
            get
            {
                return !(jsonStr == null);
            }
        }
    }
}

测试类如下:

namespace SearchService.Tests
{
    [TestClass]
    public class SearchServiceClassTest
    {
        [TestMethod]
        public void SearchServiceClassConstructor()
        {
           var searchServiceClass = new SearchServiceClass(MockStatelessServiceContextFactory.Default);
           Assert.IsTrue(searchServiceClass.IsDataJsonLoaded);
        }
    }
}

我得到的异常是" System.NullReferenceException:对象引用未设置为对象的实例."这是由于"dataPackage.Path"在"var dataPackage = Context.CodePackageActivationContext.GetDataPackageObject("Data");"中未将其设置为有效值.线.

The exception I get is that "System.NullReferenceException: Object reference not set to an instance of an object." which is arising from the fact that "dataPackage.Path" is not being set to a valid value in the "var dataPackage = Context.CodePackageActivationContext.GetDataPackageObject("Data");" line.

如何使用模拟复制CodePackageActivationContext?所述数据"包括:这里的"DataPackage"是指名称为"Data"的文件夹.并包含SearchServiceClass的代码.

How do I replicate the CodePackageActivationContext using the mock? The "Data" here refers to DataPackage which is a folder by the name "Data" and resides with the code for the SearchServiceClass.

推荐答案

您可以使用下面的代码来模拟该行:

You can use below piece of code to mock the line:

var codePackageActivationContext = new Mock<ICodePackageActivationContext>();

有关更多信息,请查看以下内容:

For more information, check this out: How to use Moq framework to unit test azure service fabrics?

对于以下查询:

在这里,它是关于一个简单的C#代码的:在私有字符串jsonStr; 行中,定义了jsonStr.但是,在引用它之前,您应该对其进行初始化,否则它将引发空引用错误-您正在构造函数中执行此操作.

Here, it is about a simple C# code : In the line private string jsonStr;, jsonStr is defined. But, before referencing it, you should initialize it, otherwise it will throw null reference error - which you are doing in your constructor.

这篇关于有关使用Mocks进行单元测试Service Fabric应用程序的特定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 21:51