views:

166

answers:

2

I have been asked to assist in coding a number of MS Unit Tests for a number of object to object mappings. 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.

Note: Unfortunately due to the nature of the source data using a mapping framework like AutoMapper will not add any value to the project.

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
}
+1  A: 

Your tests look fairly reasonably (when you implement TODO: test for an incorrect "SomeAbbreviatedType" property value), there is not much more you can test. One thing I would personally look into a bit more is error handling, e.g. is it valid for Bar to have SomeAbbreviatedType empty? Maybe should it be an enum, etc.? But from testing perspective its alright.

Grzenio
Thanks for confirming my approach
Kane
A: 

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.

Ira Baxter