c # unit testing, static methods (static) method private (Private) and unit testing

Using Reflected:

/// <Summary> 
        /// call the static method 
        /// </ Summary> AKF 
        /// <param name = "T"> full name of the class </ param> 

        /// <paramName = "strMethod"> method Name </ param> 

        /// <paramName = "aobjParams"> parameters </ param> 

        /// <returns> function return value </ returns> 

        public static Object RunStaticMethod (the System.Type T, strMethod String, Object [] aobjParams ) 
        { 

            the BindingFlags EFLAGS = 

            BindingFlags.Static | BindingFlags.Public | 

             BindingFlags.NonPublic; 

            return RunMethod (T, strMethod, 

             null, aobjParams, EFLAGS); 

        } 

        /// <Summary>

        Examples of the method call /// 

        /// </ summary>

        /// <param name="t">类全名</param>

        /// <paramname="strMethod">方法名</param>

        /// <paramname="objInstance">类的实例</param>

        ///<paramname="aobjParams">参数表</param>

        ///<returns>函数返回值</returns>



        public static object RunInstanceMethod(System.Type t, string strMethod,

         object objInstance, object[] aobjParams)
        {

            BindingFlags eFlags = BindingFlags.Instance | BindingFlags.Public |

             BindingFlags.NonPublic;

            return RunMethod(t, strMethod,

             objInstance, aobjParams, eFlags);

        }



        private static object RunMethod(System.Type t, string

         strMethod, object objInstance, object[] aobjParams, BindingFlags eFlags)
        {

            MethodInfo m;

            try
            {

                m = t.GetMethod(strMethod, eFlags);

                if (m == null)
                {

                    throw new ArgumentException("There is no method '" +

                     strMethod + "' for type'" + t.ToString() + "'.");

                }

                object objRet = m.Invoke(objInstance, aobjParams);

                return objRet;

            }

            catch
            {

                throw;

            }

        }

  

During the test examples:

If there are two methods in the windows form1, respectively, a static method and a private instance as follows:

private static string StaticMethod(string s, int i)
        {


            return  s+i;
        
        }


        private string InstanceMethod(string s, int i)
        {


            return s + i;

        }

  

 

Test code can call:

 

[TestMethod]
public void TestInstanceMethod()
{

Form1 f = new Form1();
//f.msg(f);
//f.button1_Click(null, null);

string ret = "" + RunInstanceMethod(typeof(Form1), "InstanceMethod", f, new object[] { "123", 123 });

Assert.AreEqual(ret, "123123");

}


[TestMethod]
public void TestStaticMethod()
{

string ret = "" + RunStaticMethod(typeof(Form1), "StaticMethod", new object[] { "123", 123 });

Assert.AreEqual(ret, "123123");


}

 

Guess you like

Origin www.cnblogs.com/wgscd/p/11586371.html