Vincent Grondin .NET Solution ArchitectC# Most Valuable ProfessionalUser Group Leader .NET MontrealComparing 3 mocking frameworks
1 license of Isolator to win…Go to:   http://www.typemock.com/winIn the User Group box, enter :         Vincent Grondin VCC1
Content descriptionWhy should we mock behaviors?Existing mocking framework familiesReview of the application we are using todayExploring different mocking frameworksWithout mocking frameworkMoqJustMockIsolator2
3Why should we mock behaviors?
Why should we mock behaviors?Isolate the code to test fromit’s infrastructureRender your code predictableAccelerate unit tests executionSimulate the execution of parts of your codeFacilitate parallel developments4
Existing mocking framework familiesPffft !  We don’t need no mocking framework, boss !Frameworks based on DynamicProxies (Castle project)Frameworks based on the .NET Profiler API5
Pffft !  We don’t need no mocking framework, boss !Easier said than done…  Deep understanding of these concepts:InterfacesPolymorphism using interfacesDependency injectionService 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
Pffft !  We don’t need no mocking framework, boss !7
There are two main families…Frameworks based on the Dynamic ProxiesWhat’s a proxy?Ok… then what’s a Dynamic Proxy?Existing frameworks?MoQRhino MocksNmock3FakeItEasyand 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
There are two main families…Frameworks based on the .NET Profiler APIWhat’s the .NET Profiler API?Always been accessible inside the CLR…OnFunctionEntered / ExitedOnExceptionUnhandledOnExceptionCatchedOnClassLoaded / UnloadedOnEventFiredExisting frameworks?JustMock from TelerikMoles from MicrosoftIsolator from TypeMockLimitations?Your imagination…   9
Review of the application we are using today10
Exploring different mocking frameworksMoq(Open Source)JustMock  (Telerik)Isolator  (TypeMock)11
Steps to follow…Have a good knowledge of the code you’re about to testUnderstand clearly what needs to be mocked in this codeIn your test, identify which mock to use to test your code12
Without mocking frameworkDemo13
Moq (Open Source)Most important class:Mock<T>Important methods and properties:Mock<T>.SetupUsed to designate the method to mock (“Arrange” section of a test)ISetup.ReturnsDetermines what will be returned when method in “Setup” is calledMock<T>.ObjectReference to the dynamically created mockDemo14
JustMock (Telerik)Most important class:Mock (static class)Important methods:Mock.Create<T>Used to create a mock of any typeMock.ArrangeUsed to designate the method to mockActionExpectation.Returns  / FuncExpectation.ReturnsDetermines what will be returned when the method named in “Arrange” is called.ActionExpectation.DoInstead  / FuncExpectation.DoInsteadReplaces the call to the method specified in “Arrange” with another method call.Demo15

Mocking vtcc3 - en

  • 1.
    Vincent Grondin .NETSolution ArchitectC# Most Valuable ProfessionalUser Group Leader .NET MontrealComparing 3 mocking frameworks
  • 2.
    1 license ofIsolator to win…Go to: http://www.typemock.com/winIn the User Group box, enter : Vincent Grondin VCC1
  • 3.
    Content descriptionWhy shouldwe mock behaviors?Existing mocking framework familiesReview of the application we are using todayExploring different mocking frameworksWithout mocking frameworkMoqJustMockIsolator2
  • 4.
    3Why should wemock behaviors?
  • 5.
    Why should wemock behaviors?Isolate the code to test fromit’s infrastructureRender your code predictableAccelerate unit tests executionSimulate the execution of parts of your codeFacilitate parallel developments4
  • 6.
    Existing mocking frameworkfamiliesPffft ! We don’t need no mocking framework, boss !Frameworks based on DynamicProxies (Castle project)Frameworks based on the .NET Profiler API5
  • 7.
    Pffft ! We don’t need no mocking framework, boss !Easier said than done… Deep understanding of these concepts:InterfacesPolymorphism using interfacesDependency injectionService 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 twomain families…Frameworks based on the Dynamic ProxiesWhat’s a proxy?Ok… then what’s a Dynamic Proxy?Existing frameworks?MoQRhino MocksNmock3FakeItEasyand 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 twomain families…Frameworks based on the .NET Profiler APIWhat’s the .NET Profiler API?Always been accessible inside the CLR…OnFunctionEntered / ExitedOnExceptionUnhandledOnExceptionCatchedOnClassLoaded / UnloadedOnEventFiredExisting frameworks?JustMock from TelerikMoles from MicrosoftIsolator from TypeMockLimitations?Your imagination… 9
  • 11.
    Review of theapplication we are using today10
  • 12.
    Exploring different mockingframeworksMoq(Open Source)JustMock (Telerik)Isolator (TypeMock)11
  • 13.
    Steps to follow…Havea good knowledge of the code you’re about to testUnderstand clearly what needs to be mocked in this codeIn your test, identify which mock to use to test your code12
  • 14.
  • 15.
    Moq (Open Source)Mostimportant class:Mock<T>Important methods and properties:Mock<T>.SetupUsed to designate the method to mock (“Arrange” section of a test)ISetup.ReturnsDetermines what will be returned when method in “Setup” is calledMock<T>.ObjectReference to the dynamically created mockDemo14
  • 16.
    JustMock (Telerik)Most importantclass:Mock (static class)Important methods:Mock.Create<T>Used to create a mock of any typeMock.ArrangeUsed to designate the method to mockActionExpectation.Returns / FuncExpectation.ReturnsDetermines what will be returned when the method named in “Arrange” is called.ActionExpectation.DoInstead / FuncExpectation.DoInsteadReplaces the call to the method specified in “Arrange” with another method call.Demo15
  • 17.
    Isolator (TypeMock)Most importantclass:Isolate (which should probably be static but isn’t)Important methods:Isolate.Fake.Instance<>Used to create a mock of any typeIsolate.WhenCalled<>Used to designate the method to mockIReturnValueHandler.WillReturnDetermines what will be returned when the method named in “WhenCalled” is called.IReturnValueHandler.DoInsteadReplaces the call to the method specified in “WhenCalled” with another method call.Demo16
  • 18.