Monday, 23 March 2009

Back to Basics – A .NET Engineer Reading C

Well it’s been a while since I delved into the realm of C code, but a project recently has me reading very old C code with the final goal to produce a modernisation of an existing project so that we can continue support of it into the future.  I suppose some people might argue that C code is very supportable, and they wouldn’t be wrong, however there are just a lot more .NET programmers out there and moving this code into a .NET environment would let our existing developers start support of this product without learning a language they have forgotten how to use.

I’m starting to remember what it was like to program in C now, reading through pages and pages of code to do tasks that I would consider trivial with more modern languages.  30 odd pages of code that simple read from a parameter file and allow the core application to retrieve the results.  Stepping through byte arrays and using structured code and global variables that are just so difficult to locate when you need them.  I wonder how I ever did it.

On the other hand, there’s something very satisfying about identifying the purpose of this code so that we may re-engineer it.  The code is not badly written, in fact some of the most talented C developers I know worked on it, it’s just that the style and form is so different from modern languages that it makes my eyes water just a little bit.

Thursday, 5 March 2009

Testing that a method has been called with Rhino Mocks and the WhenCalled method

I find that sometimes it’s necessary to test that one method calls another.  This is particularly useful when implementing the Object Method pattern to wrap up an object in a static class to allow more simple calls to the object than by using the singleton pattern.  For example I have a logging class called LogIt;

public static class LogIt
   {
       // Object method pattern.
       #region Object Method Pattern
       static ILogger _Logger;
 
       /// <summary>
       /// Initialise method, must be called by every public static method.
       /// </summary>
       private static void Initialise()
       {
           if (_Logger == null)
           {
               _Logger = new EntLibLogger();
           }
       }
 
       /// <summary>
       /// Override the logger instance with another instance of the interface.
       /// </summary>
       /// <param name="logger"></param>
       public static void OverrideInstance(ILogger logger)
       {
           _Logger = logger;
       } 
       #endregion
 
       public static void Info(string message)
       {
           Initialise();
           throw new NotImplementedException();
       }
}

And I want to test the Info method.  The info method’s only responsibility is to call ILogger’s Info(string, callingmethod) method, so this is the only thing I need to test for.  Here is one way to test it with Rhino Mocks.  If you are reading this and you know a better way, I’d love to hear it.

Here is my test initialiser, I’m using MSTest for these tests.

[TestInitialize()]
public void InitialiseTest()
{
    MockRepository mocks = new MockRepository();
    ILogger logger = mocks.StrictMock<ILogger>();
    _CallCount = 0;
 
    using (mocks.Record())
    {
        Expect
            .Call(delegate
            {
                logger.Error("", null);
            }).IgnoreArguments()
            .WhenCalled(delegate 
            {
                _CallCount++;
            })
            .Repeat.AtLeastOnce();
    }
 
    LogIt.OverrideInstance(logger);
}

This code tells the mocking framework that when we call Logger.Error(arguments) to increment the _CallCount.  Now our test can check the call count after the call.

/// <summary>
///A test for Error
///</summary>
[TestMethod()]
public void ErrorTest()
{
    LogIt.Error("Test");
    Assert.AreEqual(1, _CallCount);
}

There must be plenty of ways to do this and the example above is just the most simple way I could come up with when using Rhino Mocks.