单元测试包含逻辑的静态工厂方法

时间:2012-12-06 19:20:24

标签: c# unit-testing factory

在我开始提出问题之前,我想指出,我知道堆栈溢出有很多类似的问题。不幸的是,这些问题都没有帮助我在具体方案中找到一个好的解决方案。

问题:

我想为包含逻辑的静态工厂方法编写单元测试。我正在寻找一种方法来对这种方法进行单元测试,即使它是静态的。如果那是不可能的,也许有人可以为我的班级指出更好的设计。我也考虑过使用IoC,但考虑到单元测试时看不到优势。

守则:

public class Db
{
    private XmlMapping mapping;

    public static Db<T> Create()
    {
        var mapping = XmlMapping.Create(typeOf(T).Name);
        return new Db(mapping);
    }

    private Db(XmlMapping mapping)
    {
        this.mapping = mapping;
    }
}

public class XmlMapping //class under test
{
    public static XmlMapping Create(string filename) //method under test
    {            
        try
        {
            ValidateFilename(filename);
            //deserialize xml to object of type XmlMapping
            var result = Deserialize(filename);
            if (result.IsInValid())
                throw Exception()
            return result; 
        }
        catch (Exception)
        {
            throw new DbException();
        }
    }
}

我想要进行单元测试的方法 Create XmlMapping 类中。此方法序列化xml文件并生成XmlMapping类型的对象。我试着为序列化部分编写一个存根。但是不想在构造函数中使用Mapping类调用我的数据库工厂(构造函数注入)。

修改

我的数据库工厂是通用的。泛型类型用于确定哪个xml文件应该亮起,即: typeOf(T)= Customer - &gt; XmlMapping-File = Customer.xml

解决方案(谢谢Jeff!):

public class XmlMapping : IMapping //class under test
{
    internal static Func<Type, IMapping> DeserializeHandler { get; set; }

    static XmlMapping()
    {
        DeserializeHandler = DeserializeMappingFor;
    }

    public static IMapping Create(Type type)
    {
        try
        {
            var mapping = DeserializeHandler(type);
            if (!mapping.IsValid())
                throw new InvalidMappingException();
            return mapping;
        }
        catch (Exception ex)
        {
            throw new DataException("Failed to load mapping configuration from xml file.", ex);
        }
    }

    internal XmlMapping(IMapping mapping)
    {
        this.Query = mapping.Query;
        this.Table = mapping.Table;
        this.Entity = mapping.Entity;
        this.PropertyFieldCollection = mapping.PropertyFieldCollection;
    }

    private XmlMapping() { }
}


[TestClass]
public class MappingTests //testing class
{
    [TestMethod]
    public void Create_ValidDeserialization_ReturnsObjectInstance()
    {
        XmlMapping.DeserializeHandler = MakeFakeHandlerFor(MakeMappingStub());
        var result = XmlMapping.Create(typeof(ActivityDto));
        Assert.IsInstanceOfType(result, typeof(XmlMapping));
    }
}

1 个答案:

答案 0 :(得分:1)

我会使用假动作处理程序来帮助验证反序列化调用的内容。让我们添加一个Func委托属性,并将其默认为serialize方法。您的XmlMapping类和测试类似于:

public class XmlMapping //class under test
{

    static XmlMapping()
    {
        // Default the handler to the normal call to Deserialize
        DeserializeHandler = Deserialize;
    }

    public static XmlMapping Create(string filename) //method under test
    {
        //deserialize xml to object of type XmlMapping
        //preudocode:
        var result = DeserializeHandler(string.Format("{0}.xml",filename));
        //...
        return result;
    }

    // Abstract indirection function to allow you to swap out Deserialize implementations
    internal static Func<string, XmlMapping> DeserializeHandler { get; set; }

    private static XmlMapping Deserialize(string fileName)
    {
        return new XmlMapping();
    }

}

public class CreateTests {

    public void CallingDeserializeProperly()
    {

        // Arrange
        var called = false;
        Func<string, XmlMapping> fakeHandler = (string f) =>
        {
            called = true; // do your test of the input and put your result here
            return new XmlMapping();
        };

        // Act
        XmlMapping.DeserializeHandler = fakeHandler;
        var m = XmlMapping.Create("test");

        // Assert
        Assert.IsTrue(called);

    }

}