单元测试对象到对象映射

时间:2009-09-28 09:11:16

标签: c# unit-testing

我被要求协助为许多对象到对象映射编码许多MS单元测试。任何人都可以建议什么级别的测试适合测试映射功能?

下面显示的是一些(非生产)示例代码。

public class Foo
{
    public enum FooType
    {
        Active, Deleted, Open, Closed
    }

    public bool IsConditionMet { get; set; }
    public FooType SimpleType { get; set; }
}

public class Bar
{
    public string SomeCondition { get; set; }
    public string SomeAbbreviatedType { get; set; }
}

public static class BarMapper
{
    // sample to mapping code 
    public static Foo Map(Bar bar)
    {
        var foo = new Foo
                      {
                          IsConditionMet = bar.SomeCondition.Equals("y", StringComparison.OrdinalIgnoreCase)
                      };

        // this is sample code and not used in production
        switch (bar.SomeAbbreviatedType)
        {
            case "A":
                foo.SimpleType = Foo.FooType.Active;
                break;

            case "D":
                foo.SimpleType = Foo.FooType.Deleted;
                break;

            case "O":
                foo.SimpleType = Foo.FooType.Open;
                break;

            case "C":
                foo.SimpleType = Foo.FooType.Closed;
                break;
        }

        return foo;
    }
}

我不是在寻找单元测试的定义,如何在TDD上编写一个或一个解释,而是专门寻找一个在上面的例子中你要编写的单元测试的例子。

注意:不幸的是,由于使用像AutoMapper这样的映射框架的源数据的性质,不会为项目添加任何值。

更新:这是我在相应测试级别上的示例。

[TestMethod]
public void IsMappingToIsConditionMetCorrect()
{
    var fooIsConditionMetShouldBeTrue = BarMapper.Map(new Bar { SomeCondition = "Y" });
    var fooIsConditionMetShouldBeFalse = BarMapper.Map(new Bar { SomeCondition = "N" });
    var fooIsConditionMetShouldBeFalse_BecauseSomeConditionIsInvalid = BarMapper.Map(new Bar { SomeCondition = "SOMETHING" });

    Assert.IsTrue(fooIsConditionMetShouldBeTrue);
    Assert.IsFalse(fooIsConditionMetShouldBeFalse);
    Assert.IsFalse(fooIsConditionMetShouldBeFalse_BecauseSomeConditionIsInvalid);
}

[TestMethod]
public void TestsCanBuild()
{
    var fooAbbreviatedTypeShouldBeA = BarMapper.Map(new Bar { SomeAbbreviatedType = "A" });
    var fooAbbreviatedTypeShouldBeD = BarMapper.Map(new Bar { SomeAbbreviatedType = "D" });
    var fooAbbreviatedTypeShouldBeO = BarMapper.Map(new Bar { SomeAbbreviatedType = "O" });
    var fooAbbreviatedTypeShouldBeC = BarMapper.Map(new Bar { SomeAbbreviatedType = "C" });

    Assert.AreSame(Foo.FooType.Active, fooAbbreviatedTypeShouldBeA);
    Assert.AreSame(Foo.FooType.Deleted, fooAbbreviatedTypeShouldBeD);
    Assert.AreSame(Foo.FooType.Open, fooAbbreviatedTypeShouldBeO);
    Assert.AreSame(Foo.FooType.Closed, fooAbbreviatedTypeShouldBeC);

    // TODO: test for an incorrect "SomeAbbreviatedType" property value
}

2 个答案:

答案 0 :(得分:1)

您的测试看起来相当合理(当您实现TODO:测试不正确的“SomeAbbreviatedType”属性值时),您可以测试的内容不多。我个人会更多地考虑一件事是错误处理,例如对于Bar,将SomeAbbreviatedType设为空是否有效?也许它应该是枚举等等?但从测试的角度来看,它还没问题。

答案 1 :(得分:0)

您需要的是测试覆盖率数据,它测量测试代码与总代码的比率。虽然您展出的测试可能提供高比率,但除了猜测之外,您无法知道这一点。您可以从测试覆盖率工具中获取准确的数据。

可以在Test Coverage Tools找到许多语言(包括Java,C#,C ++,C和COBOL)的测试覆盖率工具。