battle of the mocking frameworks

Post on 11-May-2015

6.156 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

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

SELA DEVELOPER PRACTICEMay 5-9, 2013

Dror Helper

Battle of the mocking frameworks

About.Me

• Developing software (professionally) since 2002

• Disclaimer: I used to work at Typemock

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

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 - dependencies

Unit test

Code under test

Dependency Dependency

The solution – Mocking!

Unit test

Code under test

DependencyFake object(s)

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”

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

.NET Mocking frameworks

Isolation Frameworks

Open Source

Commercial

Isolator

JustMock

RhinoMocks

Moq

nSubtitute

FakeItEasy

NMock3

Free

MS Fakes

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

Choosing the right framework is important

1.Married into your code

2.Saves/waste time

3.Affects design

Round 1 - API

• Readable

• Discoverable

• Simple

• Extensive

• Error messages

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

Round 2 - Robustness

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

Future proof

Round 3

Constrained

Unconstrained

Unconstraint vs. Constraint

Isolation Frameworks

Constraint

Unconstraint

Isolator

JustMock

RhinoMocks

Moq

nSubtitute

FakeItEasy

NMock3

MS Fakes

Constrained

• Fake by inheritance• Force architecture

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

• Boxing of the unfakeable

Real object

Fake 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 constrained

Real Object

Dependency Fake

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?

Round 4 - deployment

Constraint

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

Summery – what to choose?

Thank You

top related