我应该使用数据库模拟进行单元测试

时间:2014-07-04 12:22:24

标签: c# unit-testing

我目前正在为一个应用程序编写各种单元测试。

现在,我确实有测试来检查代码是否正常工作。但我是否也应该模拟,例如:

  • 数据库不可用。
  • 数据库查询返回null。
  • 数据库查询需要很长时间才能执行。

我知道这不是一个编码问题,但我想对此有一些一般性的想法。

如果需要,我想到了以下方法:

SettingsUnavailableMock.Setup(x => x.PageRepository.All()).Throws(new Exception());
SettingsUnavailableMock.Setup(x => x.PageRepository.Get(It.IsAny<int>())).Throws(new Exception());
SettingsUnavailableMock.Setup(x => x.PageRepository.Get(It.IsAny<string>())).Throws(new Exception());
SettingsUnavailableMock.Setup(x => x.PageRepository.Refresh(It.IsAny<Page>())).Throws(new Exception());
SettingsUnavailableMock.Setup(x => x.PageRepository.Save()).Throws(new Exception());

当然,添加所有存储库。

然后在我的测试课中,我可以选择我想使用的Mock。

2 个答案:

答案 0 :(得分:1)

这实际上取决于您的代码与数据库交互的内容。

单元测试背后的理念是单独测试一个类。应该模拟其所有外部依赖项。

但是,您也可以检查您的类是否正确使用了它的依赖项。这将是交互测试

最终,如果一切听起来不错,你想检查整个系统是否一起工作。这是集成测试。

请注意,有一些图书馆允许您更轻松地执行集成测试,例如Specflow

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public interface IProductRepository
{
    List<Product> LoadProducts();
}

public class ProductRepository : IProductRepository
{
    public List<Product> LoadProducts()
    {
        // database code which returns the list of product
        return new List<Product>();
    }
}

public class StorageStatisticsGenerator
{
    private readonly IProductRepository _repository;

    public StorageStatisticsGenerator(IProductRepository repository)
    {
        _repository = repository;
    }

    public int ComputeNumberOfProducts()
    {
        var products = _repository.LoadProducts();
        return products.Count;
    }
}

鉴于以下课程,您可能想要测试不同类型的东西。

[TestFixture]
public class StorageStatisticsGeneratorTests
{
    private Mock<IProductRepository> _productRepository;
    private StorageStatisticsGenerator _statisticGenerator;

    [SetUp]
    public void Setup()
    {
        _productRepository = new Mock<IProductRepository>();
        _statisticGenerator = new StorageStatisticsGenerator(_productRepository.Object);
    }


    // In this test we test if the statistic generator works correctly
    // This is a UNIT TEST
    [Test]
    public void ComputeNumberOfProducts_Should_Returns_TheCorrectCount()
    {
        // Arrange
        _productRepository.Setup(p => p.LoadProducts()).Returns(new List<Product>
        {
            new Product(), new Product(), new Product()
        });

        // Act
        int result = _statisticGenerator.ComputeNumberOfProducts();

        // Assert
        Assert.AreEqual(3, result);
    }


    // In this test we test if the statistic generator use the repository as expected
    // This is an INTERACTION TEST, you could check corner case using "real life data"
    [Test]
    public void ComputeNumberOfProducts_Should_Use_The_Product_Repository()
    {
        // Arrange
        _productRepository.Setup(p => p.LoadProducts()).Returns(new List<Product>
        {
            new Product()
        });

        // Act
        _statisticGenerator.ComputeNumberOfProducts();

        // Assert
        _productRepository.Verify(p => p.LoadProducts());
    }



    // In this test we use the real repository this is an INTEGRATION TEST
    // You can flag this kind of slow test to run only during the night for instabce
    [Test, Category("Nightly")]
    public void ComputeNumberOfProducts_Should_Correctly_Integrate_With_ProductRepository()
    {
        // Arrange
        _statisticGenerator = new StorageStatisticsGenerator(new ProductRepository());

        // Act
        _statisticGenerator.ComputeNumberOfProducts();

        // Assert
        _productRepository.Verify(p => p.LoadProducts());
    }
}

如果您想了解更多信息,请阅读The art of unit testing

答案 1 :(得分:1)

理想情况下,你应该测试以上所有内容;但是,这取决于你的情况。就个人而言,我总是测试一切我可以切实测试的东西。

花费很长时间的查询非常现实。 不可用的数据库也非常现实。

Query返回null我不太确定;但是,如果这是一个现实的情况,那么一定要将它存根并进行测试。

更新 - 基于评论,我认为添加

是一件好事
public interface IRepository<T> where T : IRepositoryEntry, new()
{
    event EventHandler<RepositoryOperationEventArgs> InsertEvent;
    event EventHandler<RepositoryOperationEventArgs> UpdateEvent;
    event EventHandler<RepositoryOperationEventArgs> DeleteEvent;
    IList<String> PrimaryKeys { get; }

    void Insert(T Entry);
    void Update(T Entry);
    void Delete(Predicate<T> predicate);
    bool Exists(Predicate<T> predicate);
    T Retrieve(Predicate<T> predicate);

    IEnumerable<T> RetrieveAll();

}

public interface IRepositoryEntry
{
    IList<String> GetPrimaryKeys();
}

public class OracleRepository
{

    const string ConnectionString = "*"

    public static IDbConnection GetIDbConnection()
    {
        IDbConnection connection = new OracleConnection(ConnectionString).OpenConnection();
        return connection;
    }
    public IDbConnection GetConnection()
    {
        IDbConnection connection = new OracleConnection(ConnectionString).OpenConnection();
        return connection;
    }

}

public  class OracleRepository<T> : OracleRepository, IDisposable, IRepository<T> where T : RepositoryEntryBase, IRepositoryEntry, new()
{


    /// <summary>
    /// Gets all property names from a type.
    /// </summary>
    /// <returns>IEnumerable of strings</returns>
    static IEnumerable<String> GetEntryPropertyNames(Type type)
    {
        foreach (var propInfo in type.GetProperties())
            yield return propInfo.Name;
    }

    public event EventHandler<RepositoryOperationEventArgs> InsertEvent;
    public event EventHandler<RepositoryOperationEventArgs> UpdateEvent;
    public event EventHandler<RepositoryOperationEventArgs> DeleteEvent;

    #region Properties
    public IList<String> PrimaryKeys
    {
        get
        {
            return primaryKeys.AsReadOnly();
        }
        private set
        {
            primaryKeys = new List<String>(value);
        }
    }
    public IList<String> Properties { get; private set; }

    public String InsertText { get; private set; }
    public String UpdateText { get; private set; }
    public String DeleteText { get; private set; }
    public String SelectText { get; private set; }
    #endregion

    #region Fields
    IDbConnection connection;
    IDbTransaction transaction;
    List<String> primaryKeys;
    #endregion

    #region Constructors
    public OracleRepository()
    {
        PrimaryKeys = new List<String>(new T().GetPrimaryKeys());
        Properties = new List<String>(GetEntryPropertyNames(typeof(T))).AsReadOnly();
        InsertText = GenerateInsertText();
        UpdateText = GenerateUpdateText();
        SelectText = GenerateSelectText();
        DeleteText = GenerateDeleteText();
        connection = GetConnection();
    }
    #endregion

    #region Interface Implementations
    public void Insert(T Entry)
    {
        Insert(connection, Entry);
    }
    public void Update(T Entry)
    {
        Update(connection, Entry);
    }
    public void Delete(Predicate<T> predicate)
    {
        Delete(connection, predicate);
    }

    public T Retrieve(Predicate<T> predicate)
    {
        return Retrieve(connection, predicate);
    }
    public bool Exists( Predicate<T> predicate)
    {

        return Exists(connection, predicate);
    }

    public IEnumerable<T> RetrieveAll()
    {
        return RetrieveAll(connection);
    }

    public void Dispose()
    {
        if (transaction != null)
            transaction.Dispose();
        connection.Dispose();
    }
    #endregion

    #region Public Methods

    public void StartTransaction()
    {
        if (transaction != null)
            throw new InvalidOperationException("Transaction is already set. Please Rollback or commit transaction");
        transaction = connection.BeginTransaction();
    }
    public void CommitTransaction()
    {
        transaction.Commit();
        transaction.Dispose();
        transaction = null;
    }
    public void RollbackTransaction()
    {

        transaction.Rollback();
        transaction.Dispose();
        transaction = null;
    }


    public void Insert(IDbConnection connection, T Entry)
    {
        Type type = typeof(T);
        List<Object> args = new List<Object>();
        for (int i = 0; i < Properties.Count; i++)
            args.Add(type.GetProperty(Properties[i]).GetValue(Entry));
        connection.NonQuery(InsertText, args.ToArray());
        if (InsertEvent != null)
            InsertEvent(this, new OracleRepositoryOperationEventArgs() { Entry = Entry, Transaction = (transaction != null) });
    }

    public void Update(IDbConnection connection, T Entry)
    {
        Type type = typeof(T);
        List<Object> args = new List<Object>();

        foreach (var propertyName in Properties.Where(p => !PrimaryKeys.Any(k => k == p)))
            args.Add(type.GetProperty(propertyName).GetValue(Entry));
        foreach (var PropertyName in PrimaryKeys)
            args.Add(type.GetProperty(PropertyName).GetValue(Entry));

        connection.NonQuery(UpdateText, args.ToArray());
        if (UpdateEvent != null)
            UpdateEvent(this, new OracleRepositoryOperationEventArgs() { Entry = Entry, Transaction = (transaction != null) });

    }


    public void Delete(IDbConnection connection, Predicate<T> predicate)
    {
        var entryList = RetrieveAll(connection).Where(new Func<T, bool>(predicate));
        Type type = typeof(T);

        foreach(var entry in entryList)
        {
            List<Object> args = new List<Object>();
            foreach (var PropertyName in PrimaryKeys)
                args.Add(type.GetProperty(PropertyName).GetValue(entry));
            connection.NonQuery(DeleteText, args.ToArray());
            if (DeleteEvent != null)
                DeleteEvent(this, new OracleRepositoryOperationEventArgs() { Entry = null, Transaction = (transaction != null) });

        }
    }

    public T Retrieve(IDbConnection connection, Predicate<T> predicate)
    {
        return RetrieveAll(connection).FirstOrDefault(new Func<T, bool>(predicate));
    }
    public bool Exists(IDbConnection connection, Predicate<T> predicate)
    {
        return RetrieveAll(connection).Any(new Func<T, bool>(predicate));
    }

    public IEnumerable<T> RetrieveAll(IDbConnection connection)
    {
        List<T> collection = new List<T>();
        var result = connection.Query(SelectText);
        foreach (var row in result.Tuples)
            collection.Add(RepositoryEntryBase.FromPlexQueryResultTuple(new T(), row) as T);
        return collection;
    }
    #endregion

    #region Private Methods

    String GenerateInsertText()
    {
        String statement = "INSERT INTO {0}({1}) VALUES ({2})";
        //Do first entry here becasse its unique input.
        String columnNames = Properties.First();

        String delimiter = ", ";
        String bph = ":a"; 

        String placeHolders = bph + 0;

        //Start @ 1 since first entry is already done
        for (int i = 1; i < Properties.Count; i++)
        {
            columnNames += delimiter + Properties[i];
            placeHolders += delimiter + bph + i;
        }

        statement = String.Format(statement, typeof(T).Name, columnNames, placeHolders);
        return statement;
    }

    String GenerateUpdateText()
    {
        String bph = ":a"; 
        String cvpTemplate = "{0} = {1}";
        String statement = "UPDATE {0} SET {1} WHERE {2}";

        //Can only set Cols that are not a primary Keys, Get those Columns
        var Settables = Properties.Where(p => !PrimaryKeys.Any(k => k == p)).ToList();

        String cvp = String.Format(cvpTemplate, Settables.First() , bph + 0 );
        String condition = String.Format(cvpTemplate, PrimaryKeys.First(), bph + Settables.Count);

        //These are the values to be set | Start @ 1 since first entry is done above.
        for (int i = 1; i < Settables.Count; i++) 
            cvp += ", " + String.Format(cvpTemplate, Settables[i], bph + i);

        //This creates the conditions under which the values are set. | Start @ 1 since first entry is done above.
        for (int i = Settables.Count + 1; i < Properties.Count; i++)
            condition += ", " + String.Format(cvpTemplate, PrimaryKeys[i - Settables.Count], bph + i);

        statement = String.Format(statement, typeof(T).Name, cvp, condition);
        return statement;
    }

    String GenerateDeleteText()
    {
        String bph = ":a";
        String cvpTemplate = "{0} = {1}";
        String statement = "DELETE FROM {0} WHERE {1}";
        String condition = String.Format(cvpTemplate, PrimaryKeys.First(), bph + 0);

        for (int i =1; i < PrimaryKeys.Count; i++)
            condition += ", " + String.Format(cvpTemplate, PrimaryKeys[i], bph + i);

        statement = String.Format(statement, typeof(T).Name, condition);
        return statement;
    }

    String GenerateSelectText()
    {
        String statement = "SELECT * FROM {0}";
        statement = String.Format(statement, typeof(T).Name);
        return statement;
    }
    #endregion
}

这是实现IReposistoryEntry的元素的样子:

public class APPS : RepositoryEntryBase, IRepositoryEntry
{
    public int APP_ID { get; set; }
    public string AUTH_KEY { get; set; }
    public string TITLE { get; set; }
    public string DESCRIPTION { get; set; }
    public int IS_CLIENT_CUSTOM_APP { get; set; }


    public APPS() : base() {
        primaryKeys.Add("APP_ID");
    }
    public APPS(PlexQueryResultTuple plexTuple) : base(plexTuple) { }
}

public class RepositoryEntryBase
{

    public static RepositoryEntryBase FromPlexQueryResultTuple( RepositoryEntryBase reb, PlexQueryResultTuple plexTuple)
    {

        if (plexTuple.parent == null)
            throw new NotSupportedException("This Operation is Not supported by this PlexTuple.");

        Type type = reb.GetType();
        var pInfo = type.GetProperties();
        PlexQueryResult result = plexTuple.parent;

        foreach (var p in pInfo)
        {
            int index = result.Tuples.IndexOf(plexTuple);

            if (result[p.Name, index] == null)
                continue;

            var conversationType = Nullable.GetUnderlyingType(p.PropertyType) ?? p.PropertyType;
            object value = Convert.ChangeType(result[p.Name, index], (result[p.Name, index] != null)?conversationType: p.PropertyType);
            p.SetValue(reb, value);
        }
        return reb;
    }

    protected IList<String> primaryKeys;

    public RepositoryEntryBase() 
    {
        primaryKeys = new List<String>();
    }
    public RepositoryEntryBase(PlexQueryResultTuple plexTuple) : this()
    {
        FromPlexQueryResultTuple(this, plexTuple);
    }


    public IList<String> GetPrimaryKeys()
    {
        return primaryKeys;
    }
}

下面我发布了模拟数据库。这里重新认识的重要一点是,测试实际上使用了界面,我可以交换,可以很容易地将真实数据库与模拟数据库交换。我喜欢重复使用这个代码(它实际上在我的dll中)。所以我不必为每个项目重新编码数据库代码。

public class InMemoryRepository<T> : IRepository<T> where T :  IRepositoryEntry, new()
{
    //RepositoryEntryBase,
    public event EventHandler<RepositoryOperationEventArgs> InsertEvent;
    public event EventHandler<RepositoryOperationEventArgs> UpdateEvent;
    public event EventHandler<RepositoryOperationEventArgs> DeleteEvent;

    public IList<String> PrimaryKeys { get; protected set; }
    List<T> data;
    public InMemoryRepository() {
        PrimaryKeys = new List<String>(new T().GetPrimaryKeys());
        data = new List<T>();
    }

    public void  Insert(T Entry){
        if(Get(Entry) != null)
            throw new Exception("Duplicate Entry - Identical Key already exists");
        data.Add(Entry);
        if (InsertEvent != null)
            InsertEvent(this, new RepositoryOperationEventArgs() { Entry = Entry });
    }

    public void Update(T Entry){
        var obj = Get(Entry);
        if (obj == null)
            throw new Exception("Object does not exist");
        obj = Entry;
        if (UpdateEvent != null)
            UpdateEvent(this, new RepositoryOperationEventArgs() { Entry = obj });
    }

    public void Delete(Predicate<T> predicate)
    {
        data.RemoveAll(predicate);
        if (DeleteEvent != null)
            DeleteEvent(this, new RepositoryOperationEventArgs() { Entry = null });
    }

    public bool Exists(Predicate<T> predicate)
    {
        return data.Exists(predicate);
    }

    public T Retrieve(Predicate<T> predicate)
    {
        return data.FirstOrDefault(new Func<T, bool>(predicate));
    }

    public IEnumerable<T> RetrieveAll()
    {
        return data.ToArray();
    }

    T Get(T Entry)
    {
        //Returns Entry based on Identical PrimaryKeys
        Type entryType = typeof(T);
        var KeyPropertyInfo = entryType.GetProperties().Where(p => PrimaryKeys.Any(p2 => p2 == p.Name));
        foreach (var v in data)
        {
            //Assume the objects are identical by default to prevent false positives.
            Boolean AlreadyExists = true;
            foreach (var property in KeyPropertyInfo)
                if (!property.GetValue(v).Equals(property.GetValue(Entry)))
                    AlreadyExists = false;
            if (AlreadyExists)
                return v;
        }
        return default(T);
    }
}