我该如何模拟 sqlconnection 还是应该重构代码?
how do I mock sqlconnection or should I refactor the code?
我有下面的代码,我已经阅读了 and ,但我仍然不知道如何模拟以下 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 进行单元测试?
谢谢!多谢!
SqlBulkWriter
class 与实现问题紧密耦合,这使得很难单独测试 class。
知道连接字符串并不是真正的问题class,可以委托给其他服务。
类似于
public interface IDbConnectionFactory {
IDbConnection CreateConnection();
}
及其对您的 class 的实现看起来像这样
public class SqlConnectionFactory : IDbConnectionFactory {
private readonly string dbConnectionString;
public SqlConnectionFactory(string dbConnectionString) {
this.dbConnectionString = dbConnectionString;
}
public IDbConnection CreateConnection() {
return new SqlConnection(this.dbConnectionString);
}
}
SqlBulkWriter
class 现在可以重构为依赖抽象而不是具体。
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();
}
}
}
}
这使得 class 更容易被隔离测试,因为这些抽象可以被模拟并注入 class。
[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 以更好地了解如何使用该框架。
我有下面的代码,我已经阅读了
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 进行单元测试?
谢谢!多谢!
SqlBulkWriter
class 与实现问题紧密耦合,这使得很难单独测试 class。
知道连接字符串并不是真正的问题class,可以委托给其他服务。
类似于
public interface IDbConnectionFactory {
IDbConnection CreateConnection();
}
及其对您的 class 的实现看起来像这样
public class SqlConnectionFactory : IDbConnectionFactory {
private readonly string dbConnectionString;
public SqlConnectionFactory(string dbConnectionString) {
this.dbConnectionString = dbConnectionString;
}
public IDbConnection CreateConnection() {
return new SqlConnection(this.dbConnectionString);
}
}
SqlBulkWriter
class 现在可以重构为依赖抽象而不是具体。
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();
}
}
}
}
这使得 class 更容易被隔离测试,因为这些抽象可以被模拟并注入 class。
[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 以更好地了解如何使用该框架。