如何模拟连接到MS Access数据库的方法 [英] How to mock a method connecting to an MS Access database

查看:89
本文介绍了如何模拟连接到MS Access数据库的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一个示例方法,该方法检查表是否被占用.这是我第一次进行单元测试,并读到单元测试仅是隔离代码,并且对在数据库上执行的任何操作都应予以模拟,否则它将成为集成测试.如果有人能指出正确的方向,我不确定如何进行模拟.

I have an example method below, which checks whether a table is occupied. It's my first time doing unit tests and read that a unit test is code only in isolation and that any action performed on a database should be mocked out, otherwise it becomes an integration test. I'm not quite sure how to go about mocking this out though, if anyone can point me in the right direction.

 public bool isTableOccupied(string tableID)
 {
     bool tableOccupied = false;
     try
     {
         connection.Open();
         using (OleDbCommand command = new OleDbCommand())
         {
              command.Connection = connection;
              command.CommandText = "SELECT [Occupied] FROM [Table] WHERE [TableID] =@TableID";
              command.Parameters.AddRange(new OleDbParameter[] {
                  new OleDbParameter("@TableID", tableID)
                  });
                  OleDbDataReader reader = command.ExecuteReader();

                  while (reader.Read())
                  {
                      if (reader["Occupied"].ToString() == "True")
                      {
                          tableOccupied = true;
                      }
                  }
          }
          connection.Close();
      }
      catch (Exception ex)
      {
           MessageBox.Show("Error " + ex);
      }
      return tableOccupied;
}

推荐答案

为了模拟用于单元测试的方法,您将需要创建一个您的类应实现的接口,例如:

In order to mock a method out for unit testing you will need to create an Interface which your class should implement, for example:

public interface IRepository
{
    bool IsTableOccupied(string tableId);
}

public class ExampleRepository : IRepository
{
    public bool IsTableOccupied(string tableID)
    {
        // Your data access code goes here.
    }
}

第二,然后应该将Interface的实例注入"到父调用代码的方法或类中,例如:

Secondly, you should then "inject" the instance of the Interface into the method or class of the parent calling code, for example:

public class ExampleBusiness
{
    private readonly IRepository repository;

    public ExampleBusiness(IRepository repository)
    {
        this.repository = repository;
    }

    public bool IsTableOccupied(string tableId)
    {
        return this.repository.IsTableOccupied(tableId);
    }
}

然后,您可以编写单元测试并实现一个模拟框架,例如Moq,以模拟出"isTableOccupied"方法,例如:

You can then write unit tests and implement a mocking framework, such as Moq, to mock out your "isTableOccupied" method, for example:

// Create mock.
var mockRepository = new Mock<IRepository>();

// Setup to return the desired mock value. In this case, return true if any string tableId is provided.
mockRepository.Setup(x => x.IsTableOccupied(It.IsAny<string>())).Returns(true);

// Call the parent method and inject the mock.
var testBusiness = new ExampleBusiness(mockRepository.Object);

// Finally, assert that the value is as expected.
Assert.IsTrue(testBusiness.IsTableOccupied("TestId");

这篇关于如何模拟连接到MS Access数据库的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆