使用 Rhino Mocks 对 NpgsqlCommand 进行单元测试

发布于 2024-08-25 11:13:51 字数 1374 浏览 3 评论 0原文

我的单元测试不断收到以下错误:“System.InvalidOperationException:连接未打开。”

测试

[TestFixture]
public class Test
{
   [Test]
    public void Test1()
    {
        NpgsqlConnection connection = MockRepository.GenerateStub<NpgsqlConnection>();

        // Tried to fake the open connection
        connection.Stub(x => x.State).Return(ConnectionState.Open);
        connection.Stub(x => x.FullState).Return(ConnectionState.Open);

        DbQueries queries = new DbQueries(connection);

        bool procedure = queries.ExecutePreProcedure("201003");

        Assert.IsTrue(procedure);
    }
}

待测

using System.Data;
using Npgsql;

public class DbQueries
{
    private readonly NpgsqlConnection _connection;

    public DbQueries(NpgsqlConnection connection)
    {
        _connection = connection;
    }

    public bool ExecutePreProcedure(string date)
    {
        var command = new NpgsqlCommand("name_of_procedure", _connection);
        command.CommandType = CommandType.StoredProcedure;

        NpgsqlParameter parameter = new NpgsqlParameter {DbType = DbType.String, Value = date};

        command.Parameters.Add(parameter);
        command.ExecuteScalar();

        return true;
    }
 }

代码您将如何使用 Rhino Mocks 3.6 测试代码?

PS。 NpgsqlConnection 是到 PostgreSQL 服务器的连接。

My unit test keeps getting the following error: "System.InvalidOperationException: The Connection is not open."

The Test

[TestFixture]
public class Test
{
   [Test]
    public void Test1()
    {
        NpgsqlConnection connection = MockRepository.GenerateStub<NpgsqlConnection>();

        // Tried to fake the open connection
        connection.Stub(x => x.State).Return(ConnectionState.Open);
        connection.Stub(x => x.FullState).Return(ConnectionState.Open);

        DbQueries queries = new DbQueries(connection);

        bool procedure = queries.ExecutePreProcedure("201003");

        Assert.IsTrue(procedure);
    }
}

Code Under Test

using System.Data;
using Npgsql;

public class DbQueries
{
    private readonly NpgsqlConnection _connection;

    public DbQueries(NpgsqlConnection connection)
    {
        _connection = connection;
    }

    public bool ExecutePreProcedure(string date)
    {
        var command = new NpgsqlCommand("name_of_procedure", _connection);
        command.CommandType = CommandType.StoredProcedure;

        NpgsqlParameter parameter = new NpgsqlParameter {DbType = DbType.String, Value = date};

        command.Parameters.Add(parameter);
        command.ExecuteScalar();

        return true;
    }
 }

How would you test the code using Rhino Mocks 3.6?

PS. NpgsqlConnection is a connection to a PostgreSQL server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以为你会在 2024-09-01 11:13:51

您的方法存在一些问题:

  1. NpgsqlConnection 是一个类,我的猜测是状态属性不是虚拟的 - 所以您创建的存根将无法工作(正如您所注意到的0)。我认为没有任何 在您的测试中,
  2. 您实际上是在测试 NpgsqlCommand 的内部(因为您在 ExecuteProcedure 方法中创建了一个具体实现),这是一个第三方类,您应该只假设有关其记录的接口的任何内容,而不是实现细节(例如,它在连接上使用 State 属性)

所以我的方法是不运行此类的任何单元测试,因为它实际上只是第三方库的包装器,而是进行集成测试(因此创建一个测试数据库并连接到它)。 。

There are a few issues with your approach:

  1. NpgsqlConnection is a class, and my guess is that the state properties are not virtual - so the stub you created is just not going to work (as you noticed0. I don't think there is any workaround.
  2. In your test you are really testing the internals of NpgsqlCommand (because you create a concrete implementation in ExecuteProcedure method). This is a third party class and you should only assume anything about its documented interface, and not the implementation details (e.g. that it uses State property on connection)

So my approach would be to not run any unit tests for this class, because its effectively just a wrapper around a 3rd party library. Go for integration tests instead (so create a test database and connect to it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文