views:

196

answers:

1

I have been working on a wrapper for a COM object that can only take and return strings. The interface for the COM object looks like this:

    interface IMapinfo
    {
        void Do(string cmd);
        string Eval(string cmd);
    }

Now I have made classes that wrap up basic functions like so:

    public class Table 
    {
        IMapinfo MI;
        public string Name
        {
            //pass the command to the COM object and get back the name.
            get{return MI.Eval("TableInfo(1,1")");}
        }

    }

Now I would like to do unit testing on these classes without having to create the real COM object every time,set up the world then run the tests. So I have been looking into using mock objects but I am a little bit confused on how I would use mocking in this situation.

I am planning on using Moq, so I have written this test like this:

        [Test]
        public void MockMapinfo()
        {
            Moq.Mock<Table> MockTable = new Moq.Mock<Table>();
            MockTable.ExpectGet(n => n.Name)
                .Returns("Water_Mains");

            Table table = MockTable.Object;
            var tablename = table.Name;

            Assert.AreEqual("Water_Mains", tablename,string.Format("tablename is {0}",tablename));
            Table d = new Table();
         }

Is this the correct way to mock my COM object? How does this verity that the string getting sent to the eval function is correct? or am I doing it all wrong?

+2  A: 

Is this a duplicate? http://stackoverflow.com/questions/258627/how-would-i-do-tdd-with-a-com-ole-object

EDIT: Looks like you're asking the same question, but to validate your mocking code (OOPS).

You're not quite there for your mocking scenario. You're in the right that you want to isolate external dependencies, and your COM object certainly meets that criteria. Though I'm not a moq guy (I prefer RhinoMocks) the idea behind mocks is interaction testing...

Interaction testing is about how cohesive sets of objects work together. A valid test in this scenario would be to write a test for the component that is dependent on your COM object's behavior.

In this case, your "Table" which acts like a wrapper for your COM object, is also dependent on the COM object's behavior. For argument sake, let's say your Table object performs custom logic against the values that are returned from your COM object.

Now you can write isolated tests for your Table object while simulating your COM object's behavior using Mocks.

public class Table
{
   public Table(IMapInfo map)
   {
      _map = map;
   }

   public string Name
   {
      get 
      {
        string value = _map.Eval("myexpression");
        if (String.IsNullOrEmpty(value))
        {
            value = "none";
        }
        return value;
      }
   }

   private IMapInfo _map;
}

[TestFixture]
public class TableFixture // is this a pun?
{
   [Test]
   public void CanHandleNullsFromCOM()
   {
       MockRepository mocks = new MockRepository(); // rhino mocks, btw
       IMapInfo map = mocks.CreateMock<IMapInfo>();

       using (mocks.Record())
       {
          Expect.Call(map.Eval("myexpression").Return(null);
       }

       using (mocks.PlayBack())
       {
          Table table = new Table(map);
          Assert.AreEqual("none", table.Name, "We didn't handle nulls correctly.");
       }

       mocks.verify();
   }
}

Maybe your COM object throws exceptions when it's called, or maybe it doesn't handle string expressions very well. We're actually testing how our Table object interacts with the IMapInfo without being tied to the COM object's implementation. We're also enforcing a relationship between Table and IMapInfo in that the IMapInfo object must be called during the test.

Hope this helps.

bryanbcook
Soft of is, but at that time I was just using the straight COM object to pass the strings to and now I have a heap of layers that the string goes through, if that makes any sense.
Nathan W
and this time I'm more looking for "is this correct" rather then "if I should use mocks" type question
Nathan W
I've updated my answer. Your example is wrong; you're asserting that the mock framework gives you the value you supplied it, which will always pass. My example shows how to test the interaction between Table and IMapInfo.
bryanbcook
Your example would be correct if you were testing an object that interacted with Table. But to simply create a mock and then assert that the mock has the values you gave it isn't a valid test.
bryanbcook