如何重构此 C# SQL 查询以进行单元测试?
How do I refactor this C# SQL query to do a unit test?
我有这段查询数据库的代码。我想把实际的数据库代码放到一个单独的 class 中,这样我就可以在其他地方重用它。这将只留下 PassResult 值的实际读取,因此我可以在没有 SQL 代码 运行 的情况下对代码进行单元测试。我无法找到有关如何使此类代码可单元测试的参考资料。有人可以帮忙吗?
using System;
using System.Data;
using System.Data.SqlClient;
namespace CS_UI_Final_Inspection
{
public class CalibrationTestCheck
{
// declare the variables
private bool _calibrationTestPass = false;
private string _connectionString = string.Empty;
public bool CheckCalibrationTestResults(string serialNumber, IDeviceInfo deviceInfo, string mapID)
{
// get database location
DhrLocationPull dhrLocation = new DhrLocationPull();
_connectionString = dhrLocation.PullDhrLocation();
// build the query
SqlConnection calibrationCheckConnection = new SqlConnection(_connectionString);
SqlCommand calibrationCheckCommand = new SqlCommand("[MfgFloor].[GetLatestTestResultsForDeviceByTestType]",
calibrationCheckConnection);
// build the stored proc
calibrationCheckCommand.CommandType = CommandType.StoredProcedure;
calibrationCheckCommand.Parameters.Add(new SqlParameter("@SerialNumber", serialNumber));
calibrationCheckCommand.Parameters.Add(new SqlParameter("@DeviceTypeID", mapID));
calibrationCheckCommand.Parameters.Add(new SqlParameter("@TestDataMapTypeID", "C"));
calibrationCheckCommand.Connection.Open();
SqlDataReader calibrationCheckReader = calibrationCheckCommand.ExecuteReader();
// is there data?
if (calibrationCheckReader.HasRows)
{
// read the data
calibrationCheckReader.Read();
try
{
_calibrationTestPass = (bool) calibrationCheckReader["PassResult"];
}
catch (InvalidOperationException)
{
// means last element was not filled in
}
finally
{
// close refs
calibrationCheckReader.Close();
calibrationCheckCommand.Connection.Close();
calibrationCheckConnection.Close();
calibrationCheckReader.Dispose();
calibrationCheckCommand.Dispose();
calibrationCheckConnection.Dispose();
}
}
return _calibrationTestPass;
}
}
}
- 创建一个接口并实现它。
- 移动所有要测试的引用以使用接口(通过接口公开任何需要的 methods/properties)
- 让正在测试的构造函数或方法将接口作为参数。
Roy Oscherov 是这方面的好资源。 Roy Oscherov 写了一本名为 "The art of unit testing" 的好书。 Roy 的网站可以在这里找到:http://osherove.com/
我有这段查询数据库的代码。我想把实际的数据库代码放到一个单独的 class 中,这样我就可以在其他地方重用它。这将只留下 PassResult 值的实际读取,因此我可以在没有 SQL 代码 运行 的情况下对代码进行单元测试。我无法找到有关如何使此类代码可单元测试的参考资料。有人可以帮忙吗?
using System;
using System.Data;
using System.Data.SqlClient;
namespace CS_UI_Final_Inspection
{
public class CalibrationTestCheck
{
// declare the variables
private bool _calibrationTestPass = false;
private string _connectionString = string.Empty;
public bool CheckCalibrationTestResults(string serialNumber, IDeviceInfo deviceInfo, string mapID)
{
// get database location
DhrLocationPull dhrLocation = new DhrLocationPull();
_connectionString = dhrLocation.PullDhrLocation();
// build the query
SqlConnection calibrationCheckConnection = new SqlConnection(_connectionString);
SqlCommand calibrationCheckCommand = new SqlCommand("[MfgFloor].[GetLatestTestResultsForDeviceByTestType]",
calibrationCheckConnection);
// build the stored proc
calibrationCheckCommand.CommandType = CommandType.StoredProcedure;
calibrationCheckCommand.Parameters.Add(new SqlParameter("@SerialNumber", serialNumber));
calibrationCheckCommand.Parameters.Add(new SqlParameter("@DeviceTypeID", mapID));
calibrationCheckCommand.Parameters.Add(new SqlParameter("@TestDataMapTypeID", "C"));
calibrationCheckCommand.Connection.Open();
SqlDataReader calibrationCheckReader = calibrationCheckCommand.ExecuteReader();
// is there data?
if (calibrationCheckReader.HasRows)
{
// read the data
calibrationCheckReader.Read();
try
{
_calibrationTestPass = (bool) calibrationCheckReader["PassResult"];
}
catch (InvalidOperationException)
{
// means last element was not filled in
}
finally
{
// close refs
calibrationCheckReader.Close();
calibrationCheckCommand.Connection.Close();
calibrationCheckConnection.Close();
calibrationCheckReader.Dispose();
calibrationCheckCommand.Dispose();
calibrationCheckConnection.Dispose();
}
}
return _calibrationTestPass;
}
}
}
- 创建一个接口并实现它。
- 移动所有要测试的引用以使用接口(通过接口公开任何需要的 methods/properties)
- 让正在测试的构造函数或方法将接口作为参数。
Roy Oscherov 是这方面的好资源。 Roy Oscherov 写了一本名为 "The art of unit testing" 的好书。 Roy 的网站可以在这里找到:http://osherove.com/