Handwritten Fakes and Mocking frameworks

Not quite long did I get into the world of automated testing and ever since I’ve enjoyed it for a lot of reasons. I read Professional Test-Driven Development with C# and watched Julie Lerman’s Automated Testing course on pluralsight and these are good resources that got me started with TDD (art of unit testing is a good book I’d recommend for beginners). But my concern is about Fakes, Stubs, and Mocks. For some beginners it might be hard to understand these concepts and apply them appropriately. For some, they let the frameworks drive their understanding of these concepts. The distinction between these concepts is important because a lot of tools/frameworks, and articles describe these terms differently. My intent is not to redefine what these terms are but rather an advice for some beginners or those about to dive into this.

Almost everybody I know use different isolation frameworks for mocks or stubs, and for beginners especially, this might be a daunting task for them. They might eventually get tired trying to understand the complexities of a particular framework and give up automated testing just to avoid the pain. I say these because I’ve been with some persons who struggled with it and as such make a bad design of their classes. I sat with a friend who complained about his test, and I discovered that his problems were how to use the framework. He knew that he was going to stub out a dependency for the code under test but because he had little understanding of Moq (that’s the framework he used), he used it badly and because of that made a mess of his code. I’ve seen these problems and my advice is “Don’t Use Isolation Frameworks” (especially those starting out with unit testing) unless when needed. Of course there are disadvantages to handwritten stubs and mocks but there are advantages too. Up until now I’ve handwritten my stubs and mocks and that has helped to better understand these terms so when I move to any isolation framework, I’d know what I’m doing (even if I’ve not found the need to). Use isolation frameworks if and when your handwritten fakes are filling up your code base and getting hard to maintain, or when you’re comfortable using them. Decide wisely when to use any tools or framework for mocking, but for people starting out with unit testing or TDD, I would advice you write your own fakes and don’t over complicate things, then later move to your preferred framework. If there’s anyone out there that is following these approach I’d like to know how it turned out for you (good or bad), and I’ll appreciate if you share your experiences by commenting on this post.


2 thoughts on “Handwritten Fakes and Mocking frameworks

  1. My team was asked to add Unit Tests to a 10 year old project.. a daunting task.
    We started with Hand-Written fakes, but eventually found that using an Isolation Framework (Moq in our case) was much more productive. Especially when it comes to “Verify” type actions (eg if you want to Verify that a Service was called without having to write a boolean variable or a counter for each method in your handwritten mock).
    My advice is to always follow SOLID principles and take small steps when doing TDD. An Isolation Framework should never cause your code to be messy. If you end up in that situation then you’ve either mixed test code into your actual logic or you’ve missed the boat somewhere.

    I agree with keeping it simple, but I think Isolation Frameworks are key to doing TDD effectively.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s