Transcript
Page 1: Battle of The Mocking Frameworks

Dror [email protected] | @dhelper | http://blog.drorhelper.com

Battle of the Mocking Frameworks

Page 2: Battle of The Mocking Frameworks

2

• Consultant @CodeValue• Developing software (professionally) since

2002

• Disclaimer: I used to work @Typemock

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

About.ME

Page 3: Battle of The Mocking Frameworks

3

This is unit testa

[Test] public void AddTest() {

var cut = new Calculator(); var result = cut.Add(2, 3);

Assert.AreEqual(5, result); }

not a

real

Page 4: Battle of The Mocking Frameworks

4

Real code has dependencies

Unit test

Code under test

Dependency Dependency

Page 5: Battle of The Mocking Frameworks

5

The solution - Mocking

Unit test

Code under test

DependencyFake object(s)

Page 6: Battle of The Mocking Frameworks

6

• Replace production logic with custom logic

• We do this in order to – Focus the test on one class only– Test Interaction– Simplify Unit test writing

Isolation

Page 7: Battle of The Mocking Frameworks

7

What is a “Mock”?

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

I prefer to call them “Fakes”

Page 8: Battle of The Mocking Frameworks

Manual/Hand-rolled mock

• Use inheritance to replace production logic.• Use configurable delegates as custom logic.

PasswordChecker

PasswordChecker(IDataAccess)CheckPassword(string, string)

DataAccess

GetUserByName(string)SaveUser(string, string)

FakeDataAccess

Page 9: Battle of The Mocking Frameworks

The problem

• Maintainability – it’s a lot of work

• Gets complicated as more functionality is required:

• Production logic inside fakes

• Error prone - how do we avoid bugs in test?

Page 10: Battle of The Mocking Frameworks

10

• Create Fake objects

• Set behavior on fake objects

• Verify method was called

• And more...

What Mocking framework can do for you?

Page 11: Battle of The Mocking Frameworks

11

Meanwhile in the .NET world

Open source

• FakeItEasy• Moq• NMock3• nSubtitute• Rhino Mocks

Free

• MS Fakes

Commercial

• Isolator• JustMock

Page 12: Battle of The Mocking Frameworks

12

Usage statistics (2012)

Moq45%

Rhino Mocks23%

None9%

FakeItEasy6%

Nsubstitute6%

Isolator4%

Moles2%

MS Fakes2%

JustMocks2%

Other 1%

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

Page 13: Battle of The Mocking Frameworks

13

• Married into your (test) code

• Saves/waste development time

• Affects design

Choosing the right framework is crucial

Page 14: Battle of The Mocking Frameworks

Round 1 - API

• Readable

• Discoverable

• Simple

• Extensive

• Error messages

Page 15: Battle of The Mocking Frameworks

Mock-Demo

DB

Read Data

Request

Response

Page 16: Battle of The Mocking Frameworks

API summary

Error msg Parameters Builder 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

Error msg Parameters Builder 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 17: Battle of The Mocking Frameworks

Round 2 - Robustness

Code under test API changes• Method signature• Method name

Code under test internal changes• Unexpected call to fake• Return value from unspecified method

Future proof

Page 18: Battle of The Mocking Frameworks

Round 3

ConstrainedUnconstrained

Page 19: Battle of The Mocking Frameworks

Constrained

• Fake by inheritance• Force architecture

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

• Wrapping of the unfakeable

Real object

Fake object

Page 20: 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 21: Battle of The Mocking Frameworks

21

Constraint•NMock3•FakeItEasy•nSubtitute•Moq•RhinoMocks

Unconstraint

•Isolator•JustMock•MS Fakes

Page 22: Battle of The Mocking Frameworks

Nickels and dimes

Constraint frameworks are free – but 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 23: Battle of The Mocking Frameworks

Round 4 - deployment

Page 24: Battle of The Mocking Frameworks

Deployment – the bottom lineMS Fakes JustMock Isolator Constraint

Visual studio 2012/2013

Install on each machine.

Install on each machine.Auto-run correct version

Nothing (NuGet)

Dev machine

Only TFS (> 2012) Install on each machine.

Install or use AutoDeploy

Nothing Build Server

Only TFS (> 2012) Environment varsJustMockRunnerBuild Tasks

Environment varsTMockRunnerBuild Tasks

Just run it Run tests on build machine

Only VS profilers Use Linker Automatic Linker Works! Profilers Support

Page 25: Battle of The Mocking Frameworks

25

Which framework is better?

Page 26: Battle of The Mocking Frameworks

26

Dror HelperC: 972.05.7668543e: [email protected]: blog.drorhelper.comw: www.ozcode.net


Top Related