mocking vtcc3 - en
DESCRIPTION
Full project samples can be found here: https://skydrive.live.com/?cid=bdf9cf467011e705#!/?cid=bdf9cf467011e705&sc=documents&uc=1&id=BDF9CF467011E705%21232TRANSCRIPT
Tous droits réservés Fujitsu
Vincent Grondin .NET Solution Architect
C# Most Valuable ProfessionalUser Group Leader .NET Montreal
Comparing 3 mocking frameworks
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
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
Tous droits réservés Fujitsu4
Why should we mock behaviors?
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
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
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
Tous droits réservés Fujitsu
Pffft ! We don’t need no mocking framework, boss !
8
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
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
Tous droits réservés Fujitsu
Review of the application we are using today
11
Tous droits réservés Fujitsu
Exploring different mocking frameworks
Moq (Open Source) JustMock (Telerik) Isolator (TypeMock)
12
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
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
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
Tous droits réservés Fujitsu18