简体   繁体   English

Rhino模拟访问数据库

[英]Rhino mocks for accessing database

Please excuse me if this question has been asked before. 如果以前曾问过这个问题,请原谅。 I'm in a stage that searching for example is returning too much results that confusing me. 我正处于一个阶段,寻找的例子是返回太多令我困惑的结果。

I'm new to Rhino mocks, i know there are tons of examples but the Interface - virtual recommendation kind of confusing me. 我是犀牛嘲笑的新手,我知道有很多例子,但界面 - 虚拟推荐让我感到困惑。

If my classes works fine (but i'm obliged to unit test each line of code) do i have to adapt my code to satisfy unit Testing? 如果我的类工作正常(但我必须对每行代码进行单元测试)我是否必须调整我的代码以满足单元测试? or should it be the other way around? 或者应该是相反的方式?

To get to the point, i have the following class and i would like to mock it. 为了达到目的,我有以下课程,我想嘲笑它。

public class DatabaseOperation
{
    public IList<T> GetRecords<T>(string connectionString, string storedProcedureName)
    {
        using (var connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (var command = connection.CreateCommand())
            {
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = storedProcedureName;
                var reader = command.ExecuteReader();
                var fillSelection = new FillSelection<T>();
                return fillSelection.GetSelection(reader);
            }
        }
    }
}

How can i do that? 我怎样才能做到这一点?

There are always certain parts that cannot be really unit tested with a framework like rhino, you can test it with a framework like TypeMock Isolator but this one is not free. 总有一些部分无法使用像rhino这样的框架进行单元测试,你可以使用像TypeMock Isolator这样的框架进行测试,但这个部分不是免费的。

If you want you can test this code but that will be an integration test instead an unitary one, that means that you need an real database with fake data to pass this test. 如果你想要,你可以测试这段代码,但这将是一个集成测试,而不是一个单一的,这意味着你需要一个真假的数据库来传递这个测试。 That will give you 100% code coverage but you have to be aware that it can be broken not only by this part of code, but also with a problem related with the integration. 这将为您提供100%的代码覆盖率,但您必须意识到它不仅可以被这部分代码破坏,而且还可以解决与集成相关的问题。

I have a few software projects with both unitary and integration automatic testing and I'm running all them with great success. 我有一些软件项目,包括单一和集成自动测试,我正在运行它们并取得了巨大的成功。

If you want to test the method itself, you should add an dependency into it. 如果要测试方法本身,则应在其中添加依赖项。 It means use IDbConnection instead of SqlConnection and use it as parameter instead of connection string. 这意味着使用IDbConnection而不是SqlConnection并将其用作参数而不是连接字符串。 Then you can mock the input parameter and test method behavior. 然后,您可以模拟输入参数和测试方法行为。 For example if IDbConnction.Open method is called or if all methods are called in correct order. 例如,如果调用IDbConnction.Open方法或者以正确的顺序调用所有方法。

Your method should have following signature: 您的方法应该有以下签名:

public IList<T> GetRecords<T>(IDbConnection connection, string storedProcedureName)

In your test you can then use following statements: 在您的测试中,您可以使用以下语句:

IDbConnection connectionMock = MockRepository.GenerateMock<IDateTimeProvider>(IDbConnection);

// initialize mock object and your class here

yourClassInstance.GetRecords(connectionMock, yourProcedureName);

dateTimeProviderMock.AssertWasCalled(connection => connection.Open());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM