tags:

views:

96

answers:

3

I have this class called Table:

class Table
{
    public string Name
    {
        get
        {
            return this.wrapper.Eval(//some command); 
            //wrapper is pass in by the ctor and is a COM object.
        }
    }
}

which is used in this class:

class Map
{
  public Map MapTable(Table table)
  {
     return new Map(table.Name);
  }
}

I want to test MapTable command, should I be mocking Table or should I mock the wrapper object that is used by the table object.

I was thinking something like this

 Test()
 {
   Mock<ITable> mocktable = new Mock<ITable>(//pass in the wrapper object);
   mocktable.ExpectGet(n => n.Name).Returns("Hello World");

   ITable table = mocktable.object;
   Map mymap = Map.MapTable(table);
 }

Would that be correct?

+1  A: 

Beyond the fact that there is usually no one perfect testing solution, I'd first go for mocking the COM object: there should be a interface available and you'll probably want to test everything "above" it.

If the Table contains non-trivial code (e.g. within the Eval(); anything with conditionals; or e.g. parsing using a Culture) you might want to have a mock of it as you show in your example.

David Schmitt
+1  A: 

Yes your sample looks fine, if you want to test the Map object you should be mocking the Table object.

If you do it the other way around you will not test the Map constructor.

Sam Saffron
A: 

To help me understand. It looks like you just want the name from the Table, not the Table itself. In that case, why not just pass in a name and leave the Table out of it?

Steve Freeman