Battle of the mocking frameworks
Upcoming SlideShare
Loading in...5

Battle of the mocking frameworks



From Sela Developer Practice 2013 ...

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.



Total Views
Views on SlideShare
Embed Views



3 Embeds 43 34 8 1



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • Possible changes:CUT adds/remove a parameterDependency API changeAdd call to the same dependency without specifying in testChange to another overload of the same function

Battle of the mocking frameworks Battle of the mocking frameworks Presentation Transcript

  • SELA DEVELOPER PRACTICEMay 5-9, 2013Dror HelperBattle of the mocking frameworks
  • About.Me• Developing software (professionally) since 2002• Disclaimer: I used to work at Typemock• Blogger:
  • 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
  • The problem - dependenciesUnit testCode under testDependency Dependency
  • The solution – Mocking!Unit testCode under testDependencyFake object(s)
  • 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”
  • What does a mocking framework do?1. Create fake objects2. Set behavior on fake objects3. Verify method was called (Mock)4. Much more
  • .NET Mocking frameworksIsolation FrameworksOpen SourceCommercialIsolatorJustMockRhinoMocksMoqnSubtituteFakeItEasyNMock3FreeMS Fakes
  • 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
  • Choosing the right framework is important1. Married into your code2. Saves/waste time3. Affects design
  • Round 1 - API
  • API summeryError msgParametersMock<T>SPEReadability+ExplicitYesNo+Moq-ExplicitNoNo+Rhino-Mocks+ExplicitNoYes+FakeItEasy+ExplicitNoNo+nSubtitute?ExplicitYesNo-NMock3+ImplicitNoYes+Isolator-ExplicitNoYes+JustMockN/AExplicitYesNo-MS Fakes
  • Round 2 - Robustness• What happens when:• Code under test API changed• Code under test internal changeFuture proof
  • Round 3Unconstrained
  • Unconstraint vs. ConstraintIsolation FrameworksConstraintUnconstraintIsolatorJustMockRhinoMocksMoqnSubtituteFakeItEasyNMock3MS Fakes
  • Constrained• Fake by inheritance• Force architecture• Dependency injection (DI)• Code by interfaces (LSP)• Boxing of the unfakeableReal objectFake object
  • 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
  • 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?
  • Round 4 - deploymentConstraint
  • 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
  • Summery – what to choose?
  • Thank You