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.