views:

128

answers:

2

I already have a test setup up for this function but I would like to know if it's the best way to go about it. I have a method like this:

public static string RunTableInfoCommand(IMapinfoWrapper wrapper,
                                         TableInfoEnum infoToReturn)
{
  //pass the int value of infoToReturn to underlying COM object eg wrapper.
}

the TableInfoEnum looks like this(shortened version):

public enum TableInfoEnum
{
   TAB_INFO_NAME = 1,
   TAB_INFO_NUM = 2,
   TAB_INFO_TYPE = 3,
   TAB_INFO_NCOLS = 4,
   TAB_INFO_MAPPABLE = 5,
   TAB_INFO_READONLY = 6,
   TAB_INFO_TEMP = 7
 }

When I pass the enum into the COM object it will return different string results based on the enum value. eg

TAB_INFO_NAME = "Water_Mains"
TAB_INFO_NUM = "1"

The COM object will always return a string just different values based on the enum value.

How would I test that my RunTableInfoCommand is working properly, so far I have a test like this:

public void RunTableInfoCommandPassingTableNumberShouldReturnExpectedName()
{
  string ExpectedCommand = "TableInfo(1,{0})".FormatWith((int)TableInfoEnum.TAB_INFO_NAME);

  mockmapinfo.Expect(m => m.Evaluate(ExpectedCommand))
             .Returns("Water_Mains");

  string name = Table.RunTableInfoCommand(mockmapinfo.Object,
                                                    1,
                                                    TableInfoEnum.TAB_INFO_NAME);

   Assert.AreEqual("Water_Mains", name);
 }

It does work but I just feel like I'm missing something, how do you test for all the different enum types.

+1  A: 

If I understand you correctly, your method under test is supposed to pass the enum value to the COM object and return the string that the COM object returns. So the goal of the test is to make sure that the method is talking to the COM object correctly.

I would mock out the COM object to return the expected string for each enum value, then pass that to the method.

moffdub
There is like 37 enum values :(
Nathan W
Then use a Map as your mocked COM object.
moffdub
A: 

Okay well maybe make a loop then. This will go through all the enums and print them out. If you want it to be completely automatic, you'd have to put all the correct values into an array and get the computer to compare them. Either way, it looks like the one important piece you missed was a loop.

 public CheckAllEnumValues() {
   // somehow construct mockmapinfo here

   TableInfoEnum[] values = Enum.GetValues(typeof(TableInfoEnum));
   foreach(TableInfoEnum v in values) {
     string result = RunTableInfoCommand(mockmapinfo.Object, v);
     Console.WriteLine(Enum.GetName(typeof(TableInfoEnum), v) + ": " + result);
   }
 }
Ray Hidayat