mocking vtcc3 - en

16
Tous droits réservés Fujitsu Vincent Grondin .NET Solution Architect C# Most Valuable Professional User Group Leader .NET Montreal Comparing 3 mocking frameworks

Upload: vgrondin

Post on 25-May-2015

1.384 views

Category:

Business


0 download

DESCRIPTION

Full project samples can be found here: https://skydrive.live.com/?cid=bdf9cf467011e705#!/?cid=bdf9cf467011e705&sc=documents&uc=1&id=BDF9CF467011E705%21232

TRANSCRIPT

Page 1: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

Vincent Grondin .NET Solution Architect

C# Most Valuable ProfessionalUser Group Leader .NET Montreal

Comparing 3 mocking frameworks

Page 2: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

1 license of Isolator to win…

Go to: http://www.typemock.com/win In the User Group box, enter : Vincent Grondin VCC

2

Page 3: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

Content description

Why should we mock behaviors?

Existing mocking framework families

Review of the application we are using today

Exploring different mocking frameworks Without mocking framework Moq JustMock Isolator

3

Page 4: Mocking   vtcc3 - en

Tous droits réservés Fujitsu4

Why should we mock behaviors?

Page 5: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

Why should we mock behaviors?

Isolate the code to test from it’s infrastructure

Render your code predictable

Accelerate unit tests execution

Simulate the execution of parts of your code

Facilitate parallel developments

5

Page 6: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

Existing mocking framework families

Pffft ! We don’t need no mocking framework, boss ! Frameworks based on DynamicProxies (Castle project) Frameworks based on the .NET Profiler API

6

Page 7: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

Pffft ! We don’t need no mocking framework, boss !

Easier said than done… Deep understanding of these concepts: Interfaces Polymorphism using interfaces Dependency injection Service Locator « Wrapper classes » (static methods)

Maintain all your mocks into your code base plus all the code for your project…

Wait before you throw me tomatoes !!!On top of all this you’re going to have to adapt your architecture so that your code becomes testable.

7

Page 8: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

Pffft ! We don’t need no mocking framework, boss !

8

Page 9: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

There are two main families…

Frameworks based on the Dynamic Proxies What’s a proxy? Ok… then what’s a Dynamic Proxy? Existing frameworks?

• MoQ• Rhino Mocks• Nmock3• FakeItEasy• and many others…

Limitations?• Unable to mock static methods or classes.• Unable to mock “sealed - MustInherit” classes.• Unable to intercept calls to non “virtual – Overridable” methods.• Hard (but not impossible) to mock internal types (private classes).

9

Page 10: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

There are two main families…

Frameworks based on the .NET Profiler API What’s the .NET Profiler API? Always been accessible inside the CLR…

• OnFunctionEntered / Exited• OnExceptionUnhandled• OnExceptionCatched• OnClassLoaded / Unloaded• OnEventFired

Existing frameworks?• JustMock from Telerik• Moles from Microsoft• Isolator from TypeMock

Limitations?• Your imagination…

10

Page 11: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

Review of the application we are using today

11

Page 12: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

Exploring different mocking frameworks

Moq (Open Source) JustMock (Telerik) Isolator (TypeMock)

12

Page 13: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

Moq (Open Source)

Most important class: Mock<T>

Important methods and properties: Mock<T>.Setup

• Used to designate the method to mock (“Arrange” section of a test) ISetup.Returns

• Determines what will be returned when method in “Setup” is called Mock<T>.Object

• Reference to the dynamically created mock

Demo

15

Page 14: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

JustMock (Telerik)

Most important class: Mock (static class)

Important methods: Mock.Create<T>

• Used to create a mock of any type Mock.Arrange

• Used to designate the method to mock ActionExpectation.Returns / FuncExpectation.Returns

• Determines what will be returned when the method named in “Arrange” is called.

ActionExpectation.DoInstead / FuncExpectation.DoInstead• Replaces the call to the method specified in “Arrange” with another method call.

Demo16

Page 15: Mocking   vtcc3 - en

Tous droits réservés Fujitsu

Isolator (TypeMock)

Most important class: Isolate (which should probably be static but isn’t)

Important methods: Isolate.Fake.Instance<>

• Used to create a mock of any type Isolate.WhenCalled<>

• Used to designate the method to mock IReturnValueHandler.WillReturn

• Determines what will be returned when the method named in “WhenCalled” is called.

IReturnValueHandler.DoInstead• Replaces the call to the method specified in “WhenCalled” with another method call.

Demo17

Page 16: Mocking   vtcc3 - en

Tous droits réservés Fujitsu18