Vincent Grondin compares three mocking frameworks: Moq, JustMock, and Isolator. Mocking frameworks allow developers to isolate code under test from its infrastructure, make code behavior predictable, accelerate unit test execution by simulating code, and facilitate parallel development. Frameworks are either based on dynamic proxies or the .NET Profiler API. Without frameworks, developers must maintain mocks, dependency injection, and testable architecture in code. The document demonstrates mocking with each framework, describing important classes and methods for designating mocked methods and determining return values.
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Mocking vtcc3 - en
1. Vincent Grondin .NET Solution Architect C# Most Valuable Professional User Group Leader .NET Montreal Comparing 3 mocking frameworks
2. 1 license of Isolator to win… Go to: http://www.typemock.com/win In the User Group box, enter : Vincent Grondin VCC 1
3. 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 2
5. Why should we mock behaviors? Isolate the code to test fromit’s infrastructure Render your code predictable Accelerate unit tests execution Simulate the execution of parts of your code Facilitate parallel developments 4
6. 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 5
7. 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. 6
8. Pffft ! We don’t need no mocking framework, boss ! 7
9. 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 mockstaticmethods 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). 8
10. 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… 9
13. Steps to follow… Have a good knowledge of the code you’re about to test Understand clearly what needs to be mocked in this code In your test, identify which mock to use to test your code 12
15. 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 14
16. 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. Demo 15
17. 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. Demo 16