August 30, 2008

Unit testing and PInvoking pt. 2

In part one of this series of posts I explained how I usually set up PInvoke calls. I showed how to hide the calls themselves from consuming code so they don't have to duplicate the pre and postprocessing that's often needed. And I briefly showed how you could make the wrapper-calls non-static so you can mock the class they're in and that way test the consuming code. In this post I'll show exactly how to do this using the new Rhino Mocks 3.5 AAA model.

First let's see where we were,

   1: public class SetupApi
   2: {
   3:     [DllImport( "setupapi.dll", 
   4:         EntryPoint = "SetupDiClassNameFromGuid", 
   5:         CharSet = CharSet.Auto, 
   6:         SetLastError = true)]
   7:     private static extern bool SetupDiClassNameFromGuidEP(
   8:         ref Guid classGuid, StringBuilder className, UInt32 classNameSize, out UInt32 requiredSize);
  10:     public virtual bool SetupDiClassNameFromGuid(Guid classGuid, out string className)
  11:     {
  12:         // 50 is a Sensible default value, if we're lucky it's enough
  13:         uint reqSize = 50;
  14:         bool returnValue;
  15:         StringBuilder classNameBuilder = new StringBuilder((int)reqSize);
  17:         // First try.
  18:         returnValue = SetupDiClassNameFromGuidEP(
  19:             ref classGuid, classNameBuilder, (uint)classNameBuilder.Capacity, out reqSize);
  21:         if ((uint)classNameBuilder.Capacity != reqSize)
  22:         {
  23:             // call again with the right size stringbuilder
  24:             classNameBuilder.Capacity = (int)reqSize;
  25:             returnValue = SetupDiClassNameFromGuidEP(
  26:                 ref classGuid, classNameBuilder, reqSize, out reqSize);
  27:         }
  29:         className = classNameBuilder.ToString();
  30:         return returnValue;
  31:     }
  32: }

We put the DllImport in a separate class, the import itself is made private and the wrapper is public. I fixed some errors in this code. We have a different name for the DllImport so we need to specify an entrypoint. And I made the wrapper function virtual to be able to mock it.

The consuming code now looks something like this:

   1: public class Consumer
   2: {
   3:     public SetupApi SetupApi { get; protected set; }
   5:     public Consumer(SetupApi setupApi)
   6:     {
   7:         SetupApi = setupApi;
   8:     }
  10:     public void DoSomethingThatNeedsToBeTested()
  11:     {
  12:         Guid classGuid = Guid.Empty;
  13:         string className = string.Empty;
  14:         //..
  16:         SetupApi.SetupDiClassNameFromGuid(classGuid, out className);
  18:         //..
  19:     }
  20: }

I used constructor dependency injection to inject the SetupApi. The DoSomethingThatNeedsToBeTested method does stuff, calls SetupApi.dll and does some more stuff. Now let's take a look at how we can test this.

I'll use RhinoMocks with the new AAA model to mock and test the calls to the SetupApi. I can explain how I do this but I think the code speaks for itself. If I want to test if the right call is made for example I can use the following test:

   1: [Test]
   2: public void ConsumerCallsApi()
   3: {
   4:     SetupApi api = MockRepository.GenerateMock<SetupApi>();
   5:     Consumer sut = new Consumer(api);
   6:     string test;
   8:     api.Expect(x => x.SetupDiClassNameFromGuid(Guid.Empty, out test))
   9:         .IgnoreArguments()
  10:         .Return(true);
  12:     sut.DoSomethingThatNeedsToBeTested();
  14:     api.VerifyAllExpectations();
  15: }

You could hide SetupApi behind an interface if you want to create your own mock implementations instead of relying on a mocking framework. I like to use rhino mocks for most of my mocking.

In the next post in this series I'm going to look at a couple of ways to test the implementation of the wrapper function around the call. This is a bit harder and for simple wrapper functions the workarounds I show might be a bit too much trouble.

kick it on

No comments:

Post a Comment