为什么在关闭postgresql的连接状态后查询成功执行?

时间:2019-04-11 03:58:36

标签: c# postgresql nunit moq

我已按照 Mocking insert query to a MySQL Database using Moq 作为起订量的指南。就我而言,我将使用postgesql进行最小化。

测试用例通过。但是,我有一个问题:为什么在关闭postgresql的连接状态时显示“成功执行更新查询”?做moq时是预期的结果吗?如果不是预期的结果,我该怎么办?

先谢谢了。

这是控制台运行此测试后的结果。

更新设备集user_id = null WHERE camera_id ='G41140'

已建立的连接

打开前的状态:关闭

打开后的状态:关闭

更新查询成功执行。

这是我的代码的一部分

   [Test, Order(1)]
  public void UpdateTest()
        {
            //Arrange

            DumpDatabase databaseSetting = new DumpDatabase();
            //Assume, fill the right setting for db
            databaseSetting.Host = "***.***.*.**";
            databaseSetting.Port = "****";
            databaseSetting.Database = "*****";
            databaseSetting.UserName = "******";
            databaseSetting.Password = "***********";

            var commandMock = new Mock<IDbCommand>();
            commandMock
                .Setup(m => m.ExecuteNonQuery())
                .Verifiable();

            var connectionMock = new Mock<IDbConnection>();
            connectionMock
                .Setup(m => m.CreateCommand())
                .Returns(commandMock.Object);

            var connectionFactoryMock = new Mock<IDbConnectionFactory>();
            connectionFactoryMock
                .Setup(m => m.CreateConnection())
                .Returns(connectionMock.Object);
            var sut = new DumpConnection(connectionFactoryMock.Object, databaseSetting);
            // Act
            sut.Update();
            // Assert
            commandMock.Verify();


        }
 public class DumpConnection : IDbConnectionFactory
    {
        DumpDatabase _databaseSetting;

        private IDbConnectionFactory connectionFactory;
        public DumpConnection(IDbConnectionFactory connectionFactory, DumpDatabase databaseSetting)
        {
            this.connectionFactory = connectionFactory;
            this._databaseSetting = databaseSetting;
        }

        public IDbConnection CreateConnection()
        {
            return new NpgsqlConnection("Server=" + _databaseSetting.Host + ";User Id=" + _databaseSetting.UserName + "; " +
  "Password=" + _databaseSetting.Password + ";Database=" + _databaseSetting.Database + ";");
        }

        public void Update()
        {
            string query = "UPDATE DEVICES SET user_id=null WHERE camera_id = 'G41140' ";
            Console.WriteLine(query);
            using (var connection = connectionFactory.CreateConnection())
            {

                //Creates and returns a MySqlCommand object associated with the MySqlConnection. 
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = query;
                    Console.WriteLine("Established connection");
                    Console.WriteLine("State Before open : " + connection.State);
                    connection.Open();
                    Console.WriteLine("State After open : " + connection.State);
                    Console.WriteLine("Update query succesfully executed.");
                }
            }
        }

    }
    public interface IDbConnectionFactory
    {
        IDbConnection CreateConnection();

    }

1 个答案:

答案 0 :(得分:0)

在我看来,这很正确。当模拟对象时(请注意,它是接口),请记住,该对象是模拟版本。因此,当您模拟连接var connectionMock = new Mock<IDbConnection>();时,不再需要处理postgres数据库连接,而现在要处理的是模拟版本。因此,Connection.State不是数据库的连接状态,因为您实际上并未连接到数据库。您只是为了测试而假装,在此特定测试中,您只是在确认ExecuteNonQuery被调用了(这原本是您自那时以来更改的代码,因此更长)。

Connection.Open()不会执行任何操作,因为您是在模拟连接上调用它的,并且还没有配置它执行任何操作。