Question

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?

Was it helpful?

Solution

Is this a duplicate? 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top