如何模拟连接到 MS Access 数据库的方法
How to mock a method connecting to an MS Access database
我下面有一个示例方法,它检查table是否被占用。这是我第一次做单元测试,读到单元测试只是孤立的代码,对数据库执行的任何操作都应该被模拟出来,否则它就变成了集成测试。不过,如果有人能指出正确的方向,我不太确定如何去嘲笑它。
public bool isTableOccupied(string tableID)
{
bool tableOccupied = false;
try
{
connection.Open();
using (OleDbCommand command = new OleDbCommand())
{
command.Connection = connection;
command.CommandText = "SELECT [Occupied] FROM [Table] WHERE [TableID] =@TableID";
command.Parameters.AddRange(new OleDbParameter[] {
new OleDbParameter("@TableID", tableID)
});
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
if (reader["Occupied"].ToString() == "True")
{
tableOccupied = true;
}
}
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
return tableOccupied;
}
为了模拟单元测试的方法,您需要创建一个您的 class 应该实现的接口,例如:
public interface IRepository
{
bool IsTableOccupied(string tableId);
}
public class ExampleRepository : IRepository
{
public bool IsTableOccupied(string tableID)
{
// Your data access code goes here.
}
}
其次,你应该"inject"将Interface的实例"inject"放入父调用代码的方法或class中,例如:
public class ExampleBusiness
{
private readonly IRepository repository;
public ExampleBusiness(IRepository repository)
{
this.repository = repository;
}
public bool IsTableOccupied(string tableId)
{
return this.repository.IsTableOccupied(tableId);
}
}
然后您可以编写单元测试并实施模拟框架(例如 Moq)来模拟您的 "isTableOccupied" 方法,例如:
// Create mock.
var mockRepository = new Mock<IRepository>();
// Setup to return the desired mock value. In this case, return true if any string tableId is provided.
mockRepository.Setup(x => x.IsTableOccupied(It.IsAny<string>())).Returns(true);
// Call the parent method and inject the mock.
var testBusiness = new ExampleBusiness(mockRepository.Object);
// Finally, assert that the value is as expected.
Assert.IsTrue(testBusiness.IsTableOccupied("TestId");
我下面有一个示例方法,它检查table是否被占用。这是我第一次做单元测试,读到单元测试只是孤立的代码,对数据库执行的任何操作都应该被模拟出来,否则它就变成了集成测试。不过,如果有人能指出正确的方向,我不太确定如何去嘲笑它。
public bool isTableOccupied(string tableID)
{
bool tableOccupied = false;
try
{
connection.Open();
using (OleDbCommand command = new OleDbCommand())
{
command.Connection = connection;
command.CommandText = "SELECT [Occupied] FROM [Table] WHERE [TableID] =@TableID";
command.Parameters.AddRange(new OleDbParameter[] {
new OleDbParameter("@TableID", tableID)
});
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
if (reader["Occupied"].ToString() == "True")
{
tableOccupied = true;
}
}
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
return tableOccupied;
}
为了模拟单元测试的方法,您需要创建一个您的 class 应该实现的接口,例如:
public interface IRepository
{
bool IsTableOccupied(string tableId);
}
public class ExampleRepository : IRepository
{
public bool IsTableOccupied(string tableID)
{
// Your data access code goes here.
}
}
其次,你应该"inject"将Interface的实例"inject"放入父调用代码的方法或class中,例如:
public class ExampleBusiness
{
private readonly IRepository repository;
public ExampleBusiness(IRepository repository)
{
this.repository = repository;
}
public bool IsTableOccupied(string tableId)
{
return this.repository.IsTableOccupied(tableId);
}
}
然后您可以编写单元测试并实施模拟框架(例如 Moq)来模拟您的 "isTableOccupied" 方法,例如:
// Create mock.
var mockRepository = new Mock<IRepository>();
// Setup to return the desired mock value. In this case, return true if any string tableId is provided.
mockRepository.Setup(x => x.IsTableOccupied(It.IsAny<string>())).Returns(true);
// Call the parent method and inject the mock.
var testBusiness = new ExampleBusiness(mockRepository.Object);
// Finally, assert that the value is as expected.
Assert.IsTrue(testBusiness.IsTableOccupied("TestId");