Perform unit tests on a sealed class that internally contains private classes

0

I have a class of type sealed that contains some internal private classes with public methods within them.

My task is to test the public methods that are within those private classes, but I know in advance that the methods (including the void ), properties and fields of type private can not be tested . In theory, there should be public methods that make use of these private elements, but there are none (that is, I can practically not have access to what I want anywhere).

Here I leave the class I want to try. The lines I need to try are from 421 onwards. The only thing I do have access to are the public methods of the sealed class.

Thank you very much in advance. Greetings!

    
asked by ArCiGo 02.11.2017 в 07:30
source

2 answers

1

to achieve what you want you must use Reflection, here I send you the link where the subject is explained very well

link

In addition, I send you an example to perform and probe in a console-type project

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace PrivateClassPublicMethod
{
    class Program
    {
        sealed class GeneralClass
        {
            private class MyClass
            {
                public void Sum(int a, int b)
                {
                    Console.WriteLine(a+b);
                }
            }
        }

        static void Main(string[] args)
        {

            var assembly = Assembly.GetExecutingAssembly();
            var types = assembly.GetTypes();

            foreach (var type in types)
            {
                if (type.Name.Equals("MyClass"))
                {
                    var myMethod = type.GetMethod("Sum");
                    object t = Activator.CreateInstance(type);
                    var para = new object[]{1,2};

                    myMethod.Invoke(t, para);
                }
            }

            Console.ReadKey();
        }
    }
}
    
answered by 02.11.2017 в 16:14
1

Here I send you a first block. Follow the instructions, then take that .dll generated and paste it in a simple path, say on your desktop, and try the console project that I send you below as well. I hope you solve your problem.

1) Create a class library project with this first block of code that separated you:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace classOut
{
    sealed class ClassSealedL
    {
        private class MyClassPrivateLibrary
        {
            public void Sum(int a, int b)
            {
                Console.WriteLine(a+b);
            }
        }
    }
}

So far the library of classes.

2) Console project to test:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using classOut;

namespace PrivateClassPublicMethod
{
    class Program
    {

        static void Main(string[] args)
        {
            var assembly = Assembly.LoadFrom(@"C:\Users\danis\Desktop\classOut.dll");
            var types = assembly.GetTypes();


            foreach (var type in types)
            {
                if (type.Name.Equals("MyClassPrivateLibrary"))
                {
                    var myMethod = type.GetMethod("Sum");
                    object t = Activator.CreateInstance(type);
                    var para = new object[] { 10, 2 };

                    myMethod.Invoke(t, para);
                }
            }

            Console.ReadKey();
        }
    }
}
    
answered by 02.11.2017 в 21:21