Specific questions about unit-testing Service Fabric Applications using Mocks

≯℡__Kan透↙ 提交于 2021-01-05 07:07:49

问题


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);
            }
        }
    }
}

And the test class looks like:

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

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.

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.


回答1:


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?

For below query:

Some thing else I don't completely understand, are variables and members being created in the class, but before the constructor is called. For e.g. "private string jsonStr;" line does seem to get executed without fuss in the Unit Test, even though I am only calling the constructor in the Unit Test, and the "private string jsonStr;" is outside the constructor. So will the same apply to all the variables created outside the constructor?

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.



来源:https://stackoverflow.com/questions/65334180/specific-questions-about-unit-testing-service-fabric-applications-using-mocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!