|
最近遇到些困难,利用vs中的unit tet 进行对数据库设计代码测试,这个虽然自动生成了测试框架,但里面一些还是不会,比如返回DataSet expected = DataSet actual 这了返回数据怎么来做
部分源码
/// 返回数据集
/// </summary>
/// <param name="commandText"></param>
/// <param name="commandType"></param>
/// <param name="sqlParameters"></param>
/// <returns></returns>
public DataSet ExecuteQuery(string commandText, CommandType commandType, SqlParameter[] sqlParameters)
{
DataSet ds = new DataSet();
using (SqlCommand cmd = new SqlCommand())
{
PrepareConnection(commandText, commandType, cmd, sqlParameters);
SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
da.Fill(ds);
}
catch (Exception e)
{
e.StackTrace.ToString();
}
}
return ds;
}
/// <summary>
/// 参数预编译
/// </summary>
/// <param name="commandText"></param>
/// <param name="commandType"></param>
/// <param name="cmd"></param>
/// <param name="sqlParameters"></param>
private void PrepareConnection(string commandText, CommandType commandType, SqlCommand cmd, SqlParameter[] sqlParameters)
{
SqlConnection conn = new SqlConnection(DbConnectionString);
if (conn.State != ConnectionState.Open)
{
try
{
conn.Open();
}
catch (Exception e)
{
e.StackTrace.ToString();
}
}
cmd.CommandText = commandText;
cmd.Connection = conn;
cmd.CommandType = commandType;
if (sqlParameters.Length >= 1)
{
foreach (SqlParameter sqlParameter in sqlParameters)
{
if (sqlParameter.Value == null)
{
sqlParameter.Value = string.Empty;
}
cmd.Parameters.AddWithValue(sqlParameter.ParameterName, sqlParameter.Value);
}
}
}
}
}
生成测试代码
/// <summary>
///A test for ExecuteQuery
///</summary>
[TestMethod()]
public void ExecuteQueryTest()
{
DBAccessor target = new DBAccessor(); // TODO: Initialize to an appropriate value
string commandText =@" SELECT * from UserData where [Alias]='tt'" ; // TODO: Initialize to an appropriate value
CommandType commandType = CommandType.Text; // TODO: Initialize to an appropriate value
SqlParameter[] sqlParameters = new SqlParameter[]{}; // TODO: Initialize to an appropriate value
DataSet expected = ; // TODO: Initialize to an appropriate value
DataSet actual;
actual = target.ExecuteQuery(commandText, commandType, sqlParameters);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
/// <summary>
///A test for PrepareConnection
///</summary>
[TestMethod()]
[DeploymentItem("DBUtility.dll")]
public void PrepareConnectionTest()
{
DBAccessor_Accessor target = new DBAccessor_Accessor(); // TODO: Initialize to an appropriate value
string commandText = string.Empty; // TODO: Initialize to an appropriate value
CommandType commandType = new CommandType(); // TODO: Initialize to an appropriate value
SqlCommand cmd = null; // TODO: Initialize to an appropriate value
SqlParameter[] sqlParameters = null; // TODO: Initialize to an appropriate value
target.PrepareConnection(commandText, commandType, cmd, sqlParameters);
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}
/// <summary>
///A test for DbConnectionString
///</summary>
[TestMethod()]
[DeploymentItem("DBUtility.dll")]
public void DbConnectionStringTest()
{
string actual;
actual = DBAccessor_Accessor.DbConnectionString;
Assert.Inconclusive("Verify the correctness of this test method.");
}
}
}
中的string commandText = string.Empty 等参数怎么进行设置呢? |
|