原文:http://www.codeproject.com/csharp/TestNonPublicMembers.asp
Testing Protected Methods
要测试一个 protected 方法,我们的测试类需要继承包含这个 protected 方法的父类,然后在测试类中就可以公开使用这个 protected 方法了,示例如下:
假设要测试下面 ClassLibrary1.Class1 中的 MyProtectedMethod() 方法:
using
System;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
ClassLibrary1
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected int MyProtectedMethod(int val1, int val2)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return val1 + val2;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
} // end of class
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
//
end of namespace
下面是测试类代码:
using
System;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using
NUnit.Framework;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
ClassLibrary1
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for Tester.
/// </summary>
[TestFixture]
public class Tester : Class1
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[Test]
public void MyProtectedMethod_Test()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Assert.AreEqual(5, base.MyProtectedMethod(2, 3));
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
} // end of class
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
//
end of namespace
Testing Private Methods
测试 private 方法需要使用反射
假设要测试下面 ClassLibrary1.Class1 中的 MyPrivateMethod() 方法:
using
System;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
ClassLibrary1
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected int MyPrivateMethod(int val1, int val2)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return val1 + val2;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
} // end of class
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
//
end of namespace
下面是测试类代码:
using
System;
using
System.Reflection;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using
NUnit.Framework;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
ClassLibrary1
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for Tester.
/// </summary>
[TestFixture]
public class Tester
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[Test]
public void MyPrivateMethod_Test()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ClassLibrary1.Class1 class1 = new ClassLibrary1.Class1();
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
object[] aobjParams = new object[]
{ 3, 4 };
object strRet;
strRet = RunInstanceMethod( typeof(ClassLibrary1.Class1),
"MyPrivateMethod",
class1,
aobjParams
);
Assert.AreEqual(7, strRet.ToString());
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 运行静态方法
/// </summary>
/// <param name="t"></param>
/// <param name="strMethod"></param>
/// <param name="aobjParams"></param>
/// <returns></returns>
public static object RunStaticMethod(System.Type t, string strMethod,
object [] aobjParams)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BindingFlags eFlags =
BindingFlags.Static | BindingFlags.Public |
BindingFlags.NonPublic;
return RunMethod(t, strMethod,
null, aobjParams, eFlags);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 运行实例方法
/// </summary>
/// <param name="t"></param>
/// <param name="strMethod"></param>
/// <param name="objInstance"></param>
/// <param name="aobjParams"></param>
/// <returns></returns>
public static object RunInstanceMethod(System.Type t, string strMethod,
object objInstance, object [] aobjParams)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BindingFlags eFlags = BindingFlags.Instance | BindingFlags.Public |
BindingFlags.NonPublic;
return RunMethod(t, strMethod,
objInstance, aobjParams, eFlags);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 运行自定义方法
/// </summary>
/// <param name="t"></param>
/// <param name="strMethod"></param>
/// <param name="objInstance"></param>
/// <param name="aobjParams"></param>
/// <param name="eFlags"></param>
/// <returns></returns>
private static object RunMethod(System.Type t, string
strMethod, object objInstance, object [] aobjParams, BindingFlags eFlags)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MethodInfo m;
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m = t.GetMethod(strMethod, eFlags);
if (m == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException("There is no method '" +
strMethod + "' for type '" + t.ToString() + "'.");
}
object objRet = m.Invoke(objInstance, aobjParams);
return objRet;
}
catch
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw;
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
} // end of class
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
//
end of namespace