我有以下代码,我已阅读Moq and SqlConnection?和How can I stub IDBconnection,但我仍然不知道如何模拟以下sqlconnection。
public class SqlBulkWriter : ISqlBulkWriter
{
private readonly string _dbConnectionString;;
public SqlBulkWriter(string dbConnectionString)
{
this._dbConnectionString = dbConnectionString;
}
public void EmptyTable(string schema, string tableName)
{
using (var connection = new SqlConnection(this._dbConnectionString))
{
try
{
connection.Open();
using (var truncate = new SqlCommand($"TRUNCATE TABLE [{schema}].[{tableName}] ", connection))
{
truncate.ExecuteNonQuery();
}
}
catch (Exception ex)
{
throw new Exception(ex);
}
finally
{
connection.Close();
}
}
}
}
我要为EmptyTable进行单元测试,我想我应该首先模拟sqlconnection?或者我如何对EmptyTempTable进行单元测试?
谢谢!非常感谢!
答案 0 :(得分:2)
SqlBulkWriter
类与实现问题紧密相关,这使得难以单独测试类。
知道连接字符串并不是该类的关注点,可以委托给另一个服务。
像
这样的东西public interface IDbConnectionFactory {
IDbConnection CreateConnection();
}
它的类的实现看起来像这样
public class SqlConnectionFactory : IDbConnectionFactory {
private readonly string dbConnectionString;
public SqlConnectionFactory(string dbConnectionString) {
this.dbConnectionString = dbConnectionString;
}
public IDbConnection CreateConnection() {
return new SqlConnection(this.dbConnectionString);
}
}
现在可以重构 SqlBulkWriter
类来依赖抽象而不是结果。
public class SqlBulkWriter : ISqlBulkWriter {
private readonly IDbConnectionFactory connectionFactory;
public SqlBulkWriter(IDbConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
public void EmptyTable(string schema, string tableName) {
using (var connection = connectionFactory.CreateConnection()) {
try {
connection.Open();
using (var command = connection.CreateCommand()) {
command.CommandText = $"TRUNCATE TABLE [{schema}].[{tableName}] ";
command.CommandType = CommandType.Text;
int rowsAffected = command.ExecuteNonQuery();
}
} catch (Exception ex) {
throw ex;
} finally {
connection.Close();
}
}
}
}
这样可以更容易地对类进行单独测试,因为这些抽象可以被模拟并注入到类中。
[TestClass]
public class SqlBulkWriter_Should {
[TestMethod]
public void EmptyTable() {
//Arrange
var mock = new MockRepository(MockBehavior.Default);
var command = mock.OneOf<IDbCommand>();
var connection = mock.OneOf<IDbConnection>(_ => _.CreateCommand() == command);
var factory = mock.OneOf<IDbConnectionFactory>(_ => _.CreateConnection() == connection);
var subject = new SqlBulkWriter(factory);
var schema = "dbo";
var tableName = "TestTable";
//Act
subject.EmptyTable(schema, tableName);
//Assert
Mock.Get(command).Verify(_ => _.ExecuteNonQuery(), Times.Once());
}
}
参考Moq Quickstart以更好地了解如何使用框架。