Do the different frameworks have different target audiences?
Yes. Some frameworks like Microsoft Moles, TypeMock Isolator, and JustMock, allow you to be able to mock just about anything. These mocking tools are generally better for developers wanting to use them on existing legacy code since it may not be possible to refactor such into a design that is more testable.*
Traditionally, testable designs mean that the codebase needs to make liberal use of interfaces, abstract classes, virtual methods, unsealed classes, etc. Therefore, traditional mocking frameworks like Moq andRhinoMocks work well with code developed using Test Driven Development, Dependency Injection, and other such concepts. By the way, I would highly recommend using Dependency Injection as you gain much more than just testable code, but more maintainable code as well.
What factors should I consider when choosing which framework is right for my situation?
- Development Activity. Tools like Moq, and RhinoMocks are very active and popular and thus are up to date.
- Open Source Vs. Commercial. Consider the various pros and cons typical for this comparison. Cost, Support, etc…
- Maturity. How new is the tool. Is it in beta (like Microsoft Moles) or has it had several stable releases? For example, I like Moles for legacy code, but there are several bugs that need to addressed in it and there is going to be along wait before they get addressed (next release Nov. 2011).
- Documentation. There are several books and blogs that cover unit testing, mocking, auto-mocking, etc. Additionally how good is the tool’s own documentation?
- Syntax. Each tool has it’s own way of saying the same thing. See which one is a better fit for you.
- Speed. Tools that use CLR profiling (TypeMock, Moles, JustMock), can be much slower than traditional ones (Moq, RhinoMocks). This speed penalty may be an issue as you amass many unit tests. The rule of thumb is if a test takes longer than 1/10 a second it is too slow.
- Community Support. Are other developers writing other tools that extend (or work in compliment) to the mocking tool? There is a Moq.Contrib project that adds an Auto-mocking ability to Moq (which helps speed up test writing time). Better yet, there is AutoFixture, AutoFixture.AutoMoq, AutoFixture.AutoRhinoMocks, which also allows for Auto-mocking, plus anonymous variable creation.
*See Working Effectively With Legacy Code, for ways on how to slowly refactor code without tests into code that can be used with traditional testing (and mocking) tools.