ACCU London – July 2011

Ed Sykes – Mocking in C++

 

Once again we returned to the location where I first experienced an ACCU London talk – the offices of 7 City at Moorgate. The July meet-up saw us listening to Ed Sykes talk about Mocking in C++. As usual there was a good turnout with a few new faces to try and recruit in the bar opposite afterwards.

 

Ed started with a straw poll or two to try and gauge the audience and for once I fell into the minority as most of them were actively doing C++ and doing some form of modern unit testing. This allowed him to cut to the chase and get on with looking into the two mocking frameworks that he was using in his current position – MockItNow and Hippo Mocks. He acknowledged later that other frameworks had matured in the meantime, but that these two were the ones his team had focused on at the time they had started investigating the use of a 3rd party mocking framework for use with their C++ code-base.

 

First up was MockItNow which he clearly saw as a solution most suitable for those targeting the Microsoft compiler and trying to use mocking with a legacy code-base. C++ programmers have a natural tendency to want to look under the hood and fortunately he answered the obvious nagging question many of us immediately had, which was “How does it work”? Once this minor distraction was resolved we could get on with just enjoying how the tests were expressed and what features it provided. His examples were simple enough to touch on the salient points, yet still managed to show it in action by actually running a set of tests with VS2010.

 

The second offering Ed explored was Hippo Mocks, which is a newer and more actively developed framework. Whereas MockItNow provides good support for legacy code-bases where concrete classes may be the norm, Hippo Mocks targets a more modern style of C++ code where the use of Interfaces are more prevalent to separate concerns. The same examples were carried over from the previous discussion to provide continuity, but with some minor changes required to allow the mocked type to be passed via the constructor. Whereas the MockItNow examples had a dash of macro magic the Hippo Mocks ones seemed a little more conventional.

 

It’s a brave presenter that does live coding during a talk but Ed rose to the challenge to try and answer some of the questions from the audience by adapting his existing tests. Hippo Mocks showed its header-only, template-based nature nicely by spewing some unpleasant compiler errors as Ed tried to rework the tests but the speculative questions were never really going to be answered easily with the small amount of time left. Still, we did get to look inside this implementation too and Ed explained that his team had extended it relatively easily.

 

Those who have worked in other modern, reflection-capable languages like C# and Java will have been spoilt for years with mature mocking frameworks. Ed confidently showed us that C++ is finally catching up, even if some of the tools are platform specific or not quite as flexible as those available for its curly-brace cousins.

 

Chris Oldwood

09 August 2011

 

Bio

Chris started out as a bedroom coder in the 80s, writing assembler on 8-bit micros. These days it’s C++ and C# on Windows in big plush corporate offices. He is also the commentator for the Godmanchester Gala Day Duck Race and can be contacted via gort@cix.co.uk.