Visual Studio 2008 进行单元测试之测试私有方法

使用VS2008进行UnitTest 的步骤:

  1.在对象的私有方法单击右键;选择“Create Unit Tests...”

  2.生成测试代码;

  3.修改测试代码;

  4.测试;

 图示:

1.略

2.VS2008 生成的测试代码  

代码
/// <summary>
///A test for WhereFieldRelation
///</summary>
[TestMethod()]
[DeploymentItem(
"AIIMS.DBUtility.dll")]
public void WhereFieldRelationTest1()
{
PrivateObject param0
= null; // TODO: Initialize to an appropriate value
CreateSqlQuery_Accessor target = new CreateSqlQuery_Accessor(param0); // TODO: Initialize to an appropriate value
QueryItem item = null; // TODO: Initialize to an appropriate value
string expected = string.Empty; // TODO: Initialize to an appropriate value
string actual;
actual
= target.WhereFieldRelation(item);
Assert.AreEqual(expected, actual);
}

3.修改测试代码

代码
/// <summary>
///A test for WhereFieldRelation
///</summary>
[TestMethod()]
[DeploymentItem(
"AIIMS.DBUtility.dll")]
public void WhereFieldRelationTest()
{
//
//声明私有方法所属类的类型
//
PrivateType type = new PrivateType(typeof(CreateSqlQuery));
//
//声明PrivateObject对象,参数为对象实例instance和类型type
//
PrivateObject param0 = new PrivateObject(new CreateSqlQuery("Popedom"), type);
//
//实例化被测试类的一个对象(target即为CreateSqlQuery类的一个对象)
//
CreateSqlQuery_Accessor target = new CreateSqlQuery_Accessor(param0);

QueryItem item
= new QueryItem();
item.FieldName
= "PopedomName";
item.FieldValue
= "天通苑北二区";
string expected = "PopedomName = '天通苑北二区'";
string actual;
//
// 调用被测试方法,取得实际返回值
//
actual = target.WhereFieldRelation(item);
Assert.AreEqual(expected, actual);
}

原文地址:https://www.cnblogs.com/bobbychencj/p/1756493.html