battle of the mocking frameworks

22
SELA DEVELOPER PRACTICE May 5-9, 2013 Dror Helper Battle of the mocking frameworks

Upload: dror-helper

Post on 11-May-2015

6.156 views

Category:

Technology


1 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Battle of the mocking frameworks

SELA DEVELOPER PRACTICEMay 5-9, 2013

Dror Helper

Battle of the mocking frameworks

Page 2: Battle of the mocking frameworks

About.Me

• Developing software (professionally) since 2002

• Disclaimer: I used to work at Typemock

• Blogger: http://blog.drorhelper.com

Page 3: Battle of the mocking frameworks

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

Page 4: Battle of the mocking frameworks

The problem - dependencies

Unit test

Code under test

Dependency Dependency

Page 5: Battle of the mocking frameworks

The solution – Mocking!

Unit test

Code under test

DependencyFake object(s)

Page 6: Battle of the mocking frameworks

What is a “Mock object”?

“mock objects are simulated objects that mimic the behavior of real objects in controlled ways”[From Wikipedia]

I prefer to call them “Fakes”

Page 7: Battle of the mocking frameworks

What does a mocking framework do?

1.Create fake objects

2.Set behavior on fake objects

3.Verify method was called (Mock)

4.Much more

Page 8: Battle of the mocking frameworks

.NET Mocking frameworks

Isolation Frameworks

Open Source

Commercial

Isolator

JustMock

RhinoMocks

Moq

nSubtitute

FakeItEasy

NMock3

Free

MS Fakes

Page 9: Battle of the mocking frameworks

Usage statistics

Moq45%

Rhino Mocks23%

None9%

FakeItEasy6%

Nsubstitute6%

Isolator4%

Moles2%

MS Fakes2%

JustMocks2%

Other 1%

:// . / /2012/5/4/ - - - - - -http osherove com blog annual poll which isolation framework do you- - - .use if any html

Page 10: Battle of the mocking frameworks

Choosing the right framework is important

1.Married into your code

2.Saves/waste time

3.Affects design

Page 11: Battle of the mocking frameworks

Round 1 - API

• Readable

• Discoverable

• Simple

• Extensive

• Error messages

Page 12: Battle of the mocking frameworks

API summery

Error msg Parameters Mock<T> SPE Readability

+ Explicit Yes No + Moq

- Explicit No No + Rhino-Mocks

+ Explicit No Yes + FakeItEasy

+ Explicit No No + nSubtitute

? Explicit Yes No - NMock3

+ Implicit No Yes + Isolator

- Explicit No Yes + JustMock

N/A Explicit Yes No - MS Fakes

Page 13: Battle of the mocking frameworks

Round 2 - Robustness

• What happens when:• Code under test API changed• Code under test internal change

Future proof

Page 14: Battle of the mocking frameworks

Round 3

Constrained

Unconstrained

Page 15: Battle of the mocking frameworks

Unconstraint vs. Constraint

Isolation Frameworks

Constraint

Unconstraint

Isolator

JustMock

RhinoMocks

Moq

nSubtitute

FakeItEasy

NMock3

MS Fakes

Page 16: Battle of the mocking frameworks

Constrained

• Fake by inheritance• Force architecture

• Dependency injection (DI)• Code by interfaces (LSP)

• Boxing of the unfakeable

Real object

Fake object

Page 17: Battle of the mocking frameworks

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 constrained

Real Object

Dependency Fake

Page 18: Battle of the mocking frameworks

Nickels and dimes

Constraint 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?

Page 19: Battle of the mocking frameworks

Round 4 - deployment

Constraint

Page 20: Battle of the mocking frameworks

Deployment – the bottom line

MS Fakes JustMock Isolator Unconstraint

Only on VS11 Install on each machine.

Install on each machine.Auto-run correct version

Nothing (NuGet)

Dev machine

Only on TFS2012 Install on each machine.

Install or use AutoDeploy

Nothing Build Machine

Only TFS2012 Environment varsJustMockRunnerBuild Tasks

Environment varsTMockRunnerBuild Tasks

Just run it CI

Only VS11 profilers

Limited linker (nCover, dotTrace)

Use LinkerVS11 profilers

Works! Profilers Support

Page 21: Battle of the mocking frameworks

Summery – what to choose?

Page 22: Battle of the mocking frameworks

Thank You