Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C# 5.0
Tip/Trick

Moles (Shims) vs. Mock Frameworks Count - Why So Less

Rate me:
Please Sign up or sign in to vote.
4.90/5 (5 votes)
30 Dec 2014CPOL4 min read 30.6K   4   9
Moles (Shims) vs. Mock Frameworks Count - Why So Less

Introduction

Recently, I had a question: Why Moles (Shims) frameworks are so few in comparison to the numbers of Mock frameworks out there. After researching, I got my answers. I will share my findings for the quest here.

Microsoft Fakes offer stub and Shims, whereas Mocking frameworks like NMock, RhinoMocks, NSubstitute, Moq, JustMock and FakeItEasy, etc., all provide stubs and Mocks. As I have learned much about stubs already, so I tried to explore more about Shims (also called as Moles). At this time, an instant question came: What are other unit testing frameworks offering Moles (Shims)? After few searches and readings, I realized that Moles (Shims) frameworks are just few and then it came to my mind why Moles (Shims) frameworks are very few in comparison to numbers of Mock frameworks out there. I will share my finding for the quest in this tip.

Initial Appetizers

I started with List of unit testing frameworks available at Wikipedia. On that page, if you search for Mocks, there are 85 matches, but if you search for Mole/Moles/Shims, there is zero match. It surprised me.

I wondered if Microsoft is promoting Moles (Shims) framework (by providing those out of box with Visual Studio, but not a Mock framework) So there must be something better about Moles thank Mocks. I looked for more Moles frameworks and then put similar questions on Stackoverflow and Programmers site, but no luck and more to my surprise, those questions got closed by moderators.

Note: Microsoft Fakes are available only with Visual Studio Ultimate or Premium. (If you would like to confirm it, please have a look under “Testing Tools” > Microsoft Fakes (Unit Test Isolation) at Compare Visual Studio Offerings webpage.

Available Moles (Shims) Framework

There are following Moles (Shims) Framework available for .NET world:

  1. Pex and Mole from Microsoft
  2. TypeMock
  3. JustMock from Telerik

Following are there for Java world:

  1. JMock
  2. Powermock

The Answer

As per my understating so far, following are reasons why Moles (Shims) frameworks are so less:

  1. If you have written code with principle “Code to interface” and designed it well, you do not need Moles (Shims).
  2. Moles Framework does not force standards to follow and keeps you away from recognizing that you have poorly-written code that does not adhere to design principles. It can be potentially misused.
  3. Community believes that “TDD must help to understand where you have written poor code.” This is not true while using Moles because moles are actually designed to allow you to test against dependencies that are not injected or to test private methods as discussed here. So many TDD experts and community members are not in favor of using Moles (Shims) frameworks.
  4. If you have having legacy code and are implementing unit test for that using shims, you will fall into a Catch-22 dilemma as described in the last of this article. And as per Peter Provost's words in the same article: "I do recommend people go all the way......and refactor the code until the Shim-based test is no longer required. Every time you need to use something like Shims to test something, it is telling you that you have a design problem. You probably have high-coupling and low cohesion in that method."

Now why Microsoft provided Moles - It seems they do not want to compete with exiting great Mocks frameworks. Also in real life, we need to deal with legacy code which may not well designed for Mocking....in that case, Moles are very handy to test such code. This is discussed in point 4 above too. On MSDN as a best practices, it is recommended that we can use Mock and Moles frameworks together.

Useful Links

For those who want to explore more about the subject here, following are few great links which helped me a lot. Thanks to all the authors and contributors.

Conclusion

This tip is written based on my understanding so far on discussed points. I request the community to guide and share their understanding to refine and correct me if needed. Then I would like to share more learnings on the same subject as an article so I humbly request your comments and suggestions. Thanks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
I am a Software Developer working on Microsoft technologies. My interest is exploring and sharing the awesomeness of emerging technologies.

Comments and Discussions

 
QuestionMicrosoft's implementation sometimes doesn't work. Pin
Kenneth Kasajian8-Jan-15 7:02
Kenneth Kasajian8-Jan-15 7:02 
AnswerRe: Microsoft's implementation sometimes doesn't work. Pin
Snesh Prajapati8-Jan-15 11:34
professionalSnesh Prajapati8-Jan-15 11:34 
AnswerRe: Microsoft's implementation sometimes doesn't work. Pin
Member 1246202515-Apr-16 7:24
Member 1246202515-Apr-16 7:24 
AnswerRe: Microsoft's implementation sometimes doesn't work. Pin
Snesh Prajapati15-Apr-16 16:09
professionalSnesh Prajapati15-Apr-16 16:09 
GeneralMy vote of 5 Pin
f3a1-Jan-15 5:46
f3a1-Jan-15 5:46 
AnswerRe: My vote of 5 Pin
Snesh Prajapati1-Jan-15 6:02
professionalSnesh Prajapati1-Jan-15 6:02 
QuestionComparison, not compression. Pin
Erik Funkenbusch30-Dec-14 6:28
Erik Funkenbusch30-Dec-14 6:28 
AnswerRe: Comparison, not compression. Pin
Snesh Prajapati30-Dec-14 13:43
professionalSnesh Prajapati30-Dec-14 13:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.