Battle of the mocking frameworks


Published on

From Sela Developer Practice 2013

In the real world, applications are so complex, that in order to test a component in isolation, you need a new set of tools. Without using a mocking framework it is impossible to write easy to read and maintain unit tests. The problem is that there are a lot of different frameworks to choose from – each has its merits and shortcomings. How can you choose the correct framework? What is the difference between the free alternatives to the costly commercial options and why should you care about error message… By the end of this talk you'll understand why you need a mocking framework and which framework is the best for your project.

Published in: Technology, Education
  • Be the first to comment

Battle of the mocking frameworks

  1. 1. SELA DEVELOPER PRACTICEMay 5-9, 2013Dror HelperBattle of the mocking frameworks
  2. 2. About.Me• Developing software (professionally) since 2002• Disclaimer: I used to work at Typemock• Blogger:
  3. 3. This is a unit test[Test]public void AddTest(){var cut = new Calculator();var result = cut.Add(2, 3);Assert.AreEqual(5, result);}This is not a real unit test
  4. 4. The problem - dependenciesUnit testCode under testDependency Dependency
  5. 5. The solution – Mocking!Unit testCode under testDependencyFake object(s)
  6. 6. What is a “Mock object”?“mock objects are simulated objects that mimicthe behavior of real objects in controlled ways”[From Wikipedia]I prefer to call them “Fakes”
  7. 7. What does a mocking framework do?1. Create fake objects2. Set behavior on fake objects3. Verify method was called (Mock)4. Much more
  8. 8. .NET Mocking frameworksIsolation FrameworksOpen SourceCommercialIsolatorJustMockRhinoMocksMoqnSubtituteFakeItEasyNMock3FreeMS Fakes
  9. 9. Usage statisticsMoq45%Rhino Mocks23%None9%FakeItEasy6%Nsubstitute6%Isolator4%Moles2%MS Fakes2%JustMocks2%Other1%any.html-if-use-you-do-framework-isolation-which-poll-/annual4/5/2012
  10. 10. Choosing the right framework is important1. Married into your code2. Saves/waste time3. Affects design
  11. 11. Round 1 - API
  12. 12. API summeryError msgParametersMock<T>SPEReadability+ExplicitYesNo+Moq-ExplicitNoNo+Rhino-Mocks+ExplicitNoYes+FakeItEasy+ExplicitNoNo+nSubtitute?ExplicitYesNo-NMock3+ImplicitNoYes+Isolator-ExplicitNoYes+JustMockN/AExplicitYesNo-MS Fakes
  13. 13. Round 2 - Robustness• What happens when:• Code under test API changed• Code under test internal changeFuture proof
  14. 14. Round 3Unconstrained
  15. 15. Unconstraint vs. ConstraintIsolation FrameworksConstraintUnconstraintIsolatorJustMockRhinoMocksMoqnSubtituteFakeItEasyNMock3MS Fakes
  16. 16. Constrained• Fake by inheritance• Force architecture• Dependency injection (DI)• Code by interfaces (LSP)• Boxing of the unfakeableReal objectFake object
  17. 17. Unconstrained• Profiler API based• Can fake almost anything• 3rd party systems• Legacy code• Design your code not for testability• Can be used as if they are constrainedReal ObjectDependency Fake
  18. 18. Nickels and dimesConstraint frameworks are free – can you afford them?How much does it cost if every task takes 1 hour more?How much does it cost not to use unit tests?
  19. 19. Round 4 - deploymentConstraint
  20. 20. Deployment – the bottom lineMS FakesJustMockIsolatorUnconstraintOnly on VS11Install on eachmachine.Install on eachmachine.Auto-run correctversionNothing(NuGet)DevmachineOnly on TFS2012Install on eachmachine.Install or useAutoDeployNothingBuildMachineOnly TFS2012Environment varsJustMockRunnerBuild TasksEnvironment varsTMockRunnerBuild TasksJust run itCIOnly VS11profilersLimited linker(nCover, dotTrace)Use LinkerVS11 profilersWorks!ProfilersSupport
  21. 21. Summery – what to choose?
  22. 22. Thank You