Choosing a Mocking Framework

by 

| February 3, 2016 | in

As we neared the end of our BizSpark license term here at Don’t Panic Labs, we had to make some big choices when it came to licensing Visual Studio. Through BizSpark we were using Visual Studio 2013 Ultimate, which gave us access to a lot of different features. One of which, Microsoft Fakes, was heavily utilized as part of our unit testing.

Unfortunately, with rolling off of the BizSpark license we had to choose between staying with top of the line Visual Studio 2015 Enterprise edition (which includes Fakes) that costs $3000/year or “downgrading” to the Professional Edition (which does not include Fakes, Code Coverage, and Code Duplication) that costs $550/year. We also had to consider what kind of impact this could have on our clients and partners because in the long run they will be responsible for maintaining code. If they aren’t comfortable with the staggering costs to get these extra features then it would mean that all of the work we put in building unit tests and making sure the code is reliable would be for not.

We decided to go with the pared-back Professional version, which freed up $2500 a year that could be put towards other tools while still saving a lot of money. We also upgraded to Resharper Ultimate, which includes a bunch of tools like Code Coverage and Memory Profiling.

For a Fakes replacement, we looked at both Moq and Telerik JustMock Lite. One of the improvements these frameworks offered was that they allow us to do a better job of verifying that the methods we were mocking out were called without having to put an assertion inside of our mock.

During our evaluation of these tools, we looked at how easy it was to set up tests. Surprisingly, the setup for both were very similar:

Moq

var notification = new Notification();
var stub = new Mock();
stub.Setup(x => x.DeleteNotification(It.IsAny()))
    .Returns(true);
UnityCache.RegisterInstance(stub.Object);

var manager = new ContentManager();

var deleted = manager.DeleteNotification(notification);

Assert.IsTrue(deleted);
stub.Verify(x => x.DeleteNotification(It.IsAny()), Times.Once);

JustMock

var notification = new Notification();
var stub = Mock.Create();
stub.Arrange(x => x.DeleteNotification(Arg.IsAny()))
    .Returns(true)
    .OccursOnce();
UnityCache.RegisterInstance(stub);

var manager = new ContentManager();

var deleted = manager.DeleteNotification(notification);

Assert.IsTrue(deleted);
stub.Assert();

Microsoft Fakes

var notification = new Notification();
var isDeleted = false;
var stub = new StubINotificationAccessor
{
     DeleteNotificationNotification = model =>
     {
          isDeleted = true;
          return true;
     }
};
UnityCache.RegisterInstance(stub);
var manager = new ContentManager();

var deleted = manager.DeleteNotification(notification);

Assert.IsTrue(deleted);
Assert.IsTrue(isDeleted);

In the end, we settled on Telerik JustMock Lite. Its biggest selling point was the ability to upgrade to the full-featured paid version if we find the need for more advanced features in the future.

In looking back at the decisions we made, it’s disappointing to see Microsoft label something as “Professional” but remove tools that should really be used by anyone who considers themselves a professional developer. Thankfully there are other solutions out there that fill in the gaps – sometimes with even better results – when Microsoft pares down their feature sets for lower tiers. Between the additional tools we have settled on, I don’t feel like we have really lost out on any functionality that was being used on a regular basis.