2

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

问题:

我想为包含逻辑的静态工厂方法编写单元测试。我正在寻找一种对这种方法进行单元测试的方法,即使它是静态的。如果那不可能,也许有人可以为我的测试类指出更好的设计。我也考虑过使用 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 类型的对象。我试图为序列化部分写一个存根。但不想在构造函数(构造函数注入)中使用映射类调用我的数据库工厂。

编辑:

我的数据库工厂是通用的。通用类型用于确定应该响亮的 xml 文件,即:typeOf(T) = Customer --> XmlMapping-File = Customer.xml

解决方案(感谢杰夫!):

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));
    }
}
4

1 回答 1

1

我会使用一个假动作处理程序来帮助验证反序列化调用的内容。让我们添加一个 Func 委托属性并将其默认为您的序列化方法。您的 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);

    }

}
于 2012-12-06T19:58:21.220 回答