在私有方法中调用的模拟数据库对象

时间:2013-09-12 12:06:09

标签: c# unit-testing mocking

我已经开始尝试使用模拟框架,因为我想为(间接)将某些内容写入数据库的方法编写一些单元测试。 这是类结构的一个小例子:

class SomePersistenceClass
{
    public void Persist(object value, string type)
    {
        if (type.Equals("Numeric"))
        {
            PersistNumeric( (int)value );
        }
    }

    private void PersistNumeric(int number)
    {
        //Some Calculations and Creation of DBObject
        number++;
        var dbObject = new DatabaseObject {DbObjectData = new DatabaseObjectData {Number = number, Creator = "John Doe"}};
        PersistImpl(dbObject);
    }

    private void PersistImpl(IDatabaseObject dbObject)
    {
        try
        {
            dbObject.Save();
        }
        catch (Exception)
        {
            //ErrorHandling
            throw;
        }
    }
}

interface IDatabaseObject
{
    DatabaseObjectData DbObjectData { get; set; }
    void Save();
}

class DatabaseObject : IDatabaseObject
{
    public DatabaseObjectData DbObjectData { get; set; }

    public void Save()
    {
        //Save to Database;
    }
}

class DatabaseObjectData
{
    public int Number { get; set; }
    public string Text { get; set; }
    public string Creator { get; set; }
}

我现在要做的是测试公共持久化方法。这里的问题是,我的DatabaseObject会将数据保存到数据库中。模拟DatabaseObject的save方法会很容易,但是我不确定将被模拟对象(如你所见,我对公共方法中的DatabaseObject没有任何了解)注入PersistImpl的最佳方法是什么方法。一个是将DatabaseObject创建提升到公共方法。 所以像这样:

    public void Persist(object value, string type, IDatabaseObject dbObject)
    {
        if (type.Equals("Numeric"))
        {
            PersistNumeric( (int)value, dbObject );
        }
    }

    private void PersistNumeric(int number, IDatabaseObject dbObject)
    {
        //Some Calculations and Creation of DBObject
        number++;
        dbObject.DbObjectData.Number = number;
        dbObject.DbObjectData.Creator = "John Doe";
        PersistImpl(dbObject);
    }

但我对这种灵魂很不满意。我需要在持久化类之外创建数据库对象。

有没有好办法解决这样的问题?我绝对不得不在我的单元测试中删除数据库访问。

1 个答案:

答案 0 :(得分:1)

解决问题的一种方法是将DatabaseObject对象的构造委托给工厂类:

interface IDatabaseObjectFactory
{
    IDatabaseObject Create(DatabaseObjectData data);
}

class DatabaseObjectFactory : IDatabaseObjectFactory
{
    public IDatabaseObject Create(DatabaseObjectData data)
    {
        return new DatabaseObject {DbObjectData = data };
    }
}

class SomePersistenceClass
{
    readonly IDatabaseObjectFactory _factory;

    public SomePersistenceClass()
    {
        _factory = new DatabaseObjectFactory();
    }

    public SomePersistenceClass(IDatabaseObjectFactory factory)
    {
        _factory = factory;
    }

    public void Persist(object value, string type) { /* ... */ }
    private void PersistImpl(IDatabaseObject dbObject) { /* ... */ }

    private void PersistNumeric(int number)
    {
        //Some Calculations and Creation of DBObject
        number++;
        var dbObject = _factory.Create(new DatabaseObjectData {Number = number, Creator = "John Doe"});
        PersistImpl(dbObject);
    }

}

现在,如果您测试了自己的课程,则可以传入FakeDatabaseObjectFactory,该FakeDatabaseObject会返回无法访问您数据库的DatabaseObject

(但是,我发现你使用{{1}}类处理数据库访问的方式有点......很奇怪;但这是另一个话题。)