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
}