如何模拟 sqlconnection 还是应该重构代码?

问题描述 投票:0回答:1

我有下面的代码,我已经阅读了Moq和SqlConnection?如何存根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 进行单元测试?

unit-testing ado.net moq xunit sqlconnection
1个回答
9
投票

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以更好地了解如何使用该框架。

© www.soinside.com 2019 - 2024. All rights reserved.