简体   繁体   English

单元测试对象到对象的映射

[英]Unit testing object to object mappings

I have been asked to assist in coding a number of MS Unit Tests for a number of object to object mappings. 我被要求协助为许多对象到对象的映射编码许多MS单元测试。 Can anyone advise what level of testing is appropriate to test the mapping functionality? 谁能建议适合测试映射功能的测试级别?

Shown below is some ( non production ) sample code. 下面显示的是一些( 非生产 )示例代码。

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

I'm not looking for a definition of a unit test, how to write one or an explanation on TDD but specifically looking for an example of what unit tests you'd write given the above example. 我不是在寻找单元测试的定义,如何在TDD上写一个或一个解释,而是在寻找一个给出上述示例的单元测试的例子。

Note: Unfortunately due to the nature of the source data using a mapping framework like AutoMapper will not add any value to the project. 注意:不幸的是,由于源数据的性质,使用诸如AutoMapper的映射框架不会为项目增加任何价值。

Update: This is my example on the appropriate level of testing. 更新:这是我在适当测试级别上的示例。

[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
}

Your tests look fairly reasonably (when you implement TODO: test for an incorrect "SomeAbbreviatedType" property value), there is not much more you can test. 您的测试看起来相当合理(当您实现TODO:测试不正确的“ SomeAbbreviatedType”属性值)时,您可以进行的测试就更多了。 One thing I would personally look into a bit more is error handling, eg is it valid for Bar to have SomeAbbreviatedType empty? 我个人将进一步研究的一件事是错误处理,例如Bar将SomeAbbreviatedType为空是否有效? Maybe should it be an enum, etc.? 也许应该是枚举等? But from testing perspective its alright. 但是从测试的角度来看还可以。

What you need is test coverage data, which measures the ratio of tested code to total code. 您需要的是测试覆盖率数据,该数据可衡量已测试代码与总代码的比率。 WHile your exhibited test may provide a high ratio, other than guesswork you have no way of knowing that. 虽然您所展示的测试可能会提供很高的比率,但除了猜测之外,您无从得知。 You can get accurate data from a test coverage tool. 您可以从测试覆盖率工具获得准确的数据。

Test coverage tools for many languages (including Java, C#, C++, C and COBOL) can be found at Test Coverage Tools . 在“测试覆盖率工具”中可以找到针对多种语言(包括Java,C#,C ++,C和COBOL)的测试覆盖率工具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM