SlideShare una empresa de Scribd logo
1 de 16
Vincent Grondin  .NET Solution Architect C# Most Valuable Professional User Group Leader .NET Montreal Comparing 3 mocking frameworks
1 license of Isolator to win… Go to:   http://www.typemock.com/win In the User Group box, enter :         Vincent Grondin VCC 1
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
3 Why should we mock behaviors?
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
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
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
Pffft !  We don’t need no mocking framework, boss ! 7
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
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
Review of the application we are using today 10
Exploring different mocking frameworks Moq(Open Source) JustMock  (Telerik) Isolator  (TypeMock) 11
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
Without mocking framework Demo 13
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
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

Más contenido relacionado

La actualidad más candente

Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
Joe Wilson
 
Static code analysis
Static code analysisStatic code analysis
Static code analysis
Rune Sundling
 

La actualidad más candente (20)

Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
 
AspectMock
AspectMockAspectMock
AspectMock
 
Finding and exploiting novel flaws in Java software (SyScan 2015)
Finding and exploiting novel flaws in Java software (SyScan 2015)Finding and exploiting novel flaws in Java software (SyScan 2015)
Finding and exploiting novel flaws in Java software (SyScan 2015)
 
How to improve code quality for iOS apps?
How to improve code quality for iOS apps?How to improve code quality for iOS apps?
How to improve code quality for iOS apps?
 
TestWorks Conf Robot framework - the unsung hero of test automation - Michael...
TestWorks Conf Robot framework - the unsung hero of test automation - Michael...TestWorks Conf Robot framework - the unsung hero of test automation - Michael...
TestWorks Conf Robot framework - the unsung hero of test automation - Michael...
 
Robot framework - Lord of the Rings
Robot framework - Lord of the RingsRobot framework - Lord of the Rings
Robot framework - Lord of the Rings
 
Code Obfuscation for Android & WP7
Code Obfuscation for Android & WP7Code Obfuscation for Android & WP7
Code Obfuscation for Android & WP7
 
Exception
ExceptionException
Exception
 
QA Best Practices
QA  Best PracticesQA  Best Practices
QA Best Practices
 
Ni week 2019 - LabVIEW Unit Testing Outlook & Tutorial
Ni week 2019 - LabVIEW Unit Testing Outlook & TutorialNi week 2019 - LabVIEW Unit Testing Outlook & Tutorial
Ni week 2019 - LabVIEW Unit Testing Outlook & Tutorial
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Robot Framework : Lord of the Rings By Asheesh M
Robot Framework : Lord of the Rings By Asheesh MRobot Framework : Lord of the Rings By Asheesh M
Robot Framework : Lord of the Rings By Asheesh M
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Robot framework Gowthami Goli
Robot framework Gowthami GoliRobot framework Gowthami Goli
Robot framework Gowthami Goli
 
Static code analysis
Static code analysisStatic code analysis
Static code analysis
 
클린 테스트
클린 테스트클린 테스트
클린 테스트
 
Mocking 101
Mocking 101Mocking 101
Mocking 101
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
 
Code Quality Management iOS
Code Quality Management iOSCode Quality Management iOS
Code Quality Management iOS
 
Introduction to Robot Framework
Introduction to Robot FrameworkIntroduction to Robot Framework
Introduction to Robot Framework
 

Destacado

Lambdas and Extension using VS2010
Lambdas and Extension using VS2010Lambdas and Extension using VS2010
Lambdas and Extension using VS2010
vgrondin
 
Verbien aikamuodot pp esitys
Verbien aikamuodot pp esitysVerbien aikamuodot pp esitys
Verbien aikamuodot pp esitys
krakova
 
Suora ja epäsuora esitys
Suora ja epäsuora esitysSuora ja epäsuora esitys
Suora ja epäsuora esitys
krakova
 
Luvut ja numerot tekstissä
Luvut ja numerot tekstissäLuvut ja numerot tekstissä
Luvut ja numerot tekstissä
krakova
 
Lause, virke, päälause ja sivulause
Lause, virke, päälause ja sivulauseLause, virke, päälause ja sivulause
Lause, virke, päälause ja sivulause
krakova
 
Lause, lauseke ja lauseenjäsennys
Lause, lauseke ja lauseenjäsennysLause, lauseke ja lauseenjäsennys
Lause, lauseke ja lauseenjäsennys
krakova
 
Pilkun käyttäminen
Pilkun käyttäminenPilkun käyttäminen
Pilkun käyttäminen
krakova
 

Destacado (18)

Pension 100 k+ december 2014 (slide share)
Pension 100 k+ december 2014 (slide share)Pension 100 k+ december 2014 (slide share)
Pension 100 k+ december 2014 (slide share)
 
Climate Change
Climate ChangeClimate Change
Climate Change
 
Factor-A
Factor-AFactor-A
Factor-A
 
How To DRM Digital Content Distribution with Apps
How To DRM Digital Content Distribution with AppsHow To DRM Digital Content Distribution with Apps
How To DRM Digital Content Distribution with Apps
 
Lambdas and Extension using VS2010
Lambdas and Extension using VS2010Lambdas and Extension using VS2010
Lambdas and Extension using VS2010
 
Alergía a la proteina de la leche de vaca
Alergía a la proteina de la leche de vacaAlergía a la proteina de la leche de vaca
Alergía a la proteina de la leche de vaca
 
Dermatitis del pañal
Dermatitis del pañalDermatitis del pañal
Dermatitis del pañal
 
Verbien aikamuodot pp esitys
Verbien aikamuodot pp esitysVerbien aikamuodot pp esitys
Verbien aikamuodot pp esitys
 
enfermedad de hirschsprung
enfermedad de hirschsprungenfermedad de hirschsprung
enfermedad de hirschsprung
 
Hipersensibilidad
HipersensibilidadHipersensibilidad
Hipersensibilidad
 
Suora ja epäsuora esitys
Suora ja epäsuora esitysSuora ja epäsuora esitys
Suora ja epäsuora esitys
 
Escarlatina
EscarlatinaEscarlatina
Escarlatina
 
Luvut ja numerot tekstissä
Luvut ja numerot tekstissäLuvut ja numerot tekstissä
Luvut ja numerot tekstissä
 
Write The A+ Anaylsis Paper[1]
Write The A+ Anaylsis Paper[1]Write The A+ Anaylsis Paper[1]
Write The A+ Anaylsis Paper[1]
 
Lause, virke, päälause ja sivulause
Lause, virke, päälause ja sivulauseLause, virke, päälause ja sivulause
Lause, virke, päälause ja sivulause
 
Lause, lauseke ja lauseenjäsennys
Lause, lauseke ja lauseenjäsennysLause, lauseke ja lauseenjäsennys
Lause, lauseke ja lauseenjäsennys
 
Pilkun käyttäminen
Pilkun käyttäminenPilkun käyttäminen
Pilkun käyttäminen
 
Menggambar teknik
Menggambar teknikMenggambar teknik
Menggambar teknik
 

Similar a Mocking vtcc3 - en

Clean Code Part II - Dependency Injection at SoCal Code Camp
Clean Code Part II - Dependency Injection at SoCal Code CampClean Code Part II - Dependency Injection at SoCal Code Camp
Clean Code Part II - Dependency Injection at SoCal Code Camp
Theo Jungeblut
 

Similar a Mocking vtcc3 - en (20)

Common ASP.NET Design Patterns - Telerik India DevCon 2013
Common ASP.NET Design Patterns - Telerik India DevCon 2013Common ASP.NET Design Patterns - Telerik India DevCon 2013
Common ASP.NET Design Patterns - Telerik India DevCon 2013
 
springtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdfspringtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdf
 
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckCut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
 
Metasploit framwork
Metasploit framworkMetasploit framwork
Metasploit framwork
 
Microsoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable CodeMicrosoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable Code
 
Unit Tests with Microsoft Fakes
Unit Tests with Microsoft FakesUnit Tests with Microsoft Fakes
Unit Tests with Microsoft Fakes
 
Acceleo Code Generation
Acceleo Code GenerationAcceleo Code Generation
Acceleo Code Generation
 
.NET Fundamentals and Business Application Development
.NET Fundamentals and Business Application Development.NET Fundamentals and Business Application Development
.NET Fundamentals and Business Application Development
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
 
OIVM
OIVMOIVM
OIVM
 
System verilog important
System verilog importantSystem verilog important
System verilog important
 
Medium Trust for Umbraco
Medium Trust for UmbracoMedium Trust for Umbraco
Medium Trust for Umbraco
 
Intro To AOP
Intro To AOPIntro To AOP
Intro To AOP
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Clean Code Part II - Dependency Injection at SoCal Code Camp
Clean Code Part II - Dependency Injection at SoCal Code CampClean Code Part II - Dependency Injection at SoCal Code Camp
Clean Code Part II - Dependency Injection at SoCal Code Camp
 
Lunch and learn as3_frameworks
Lunch and learn as3_frameworksLunch and learn as3_frameworks
Lunch and learn as3_frameworks
 
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
Cut your Dependencies - Dependency Injection at Silicon Valley Code CampCut your Dependencies - Dependency Injection at Silicon Valley Code Camp
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
 
Metasploit Computer security testing tool
Metasploit  Computer security testing toolMetasploit  Computer security testing tool
Metasploit Computer security testing tool
 
Elements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code FirstElements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code First
 
Enterprise Java: Just What Is It and the Risks, Threats, and Exposures It Poses
Enterprise Java: Just What Is It and the Risks, Threats, and Exposures It PosesEnterprise Java: Just What Is It and the Risks, Threats, and Exposures It Poses
Enterprise Java: Just What Is It and the Risks, Threats, and Exposures It Poses
 

Último

Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Dipal Arora
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
amitlee9823
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
amitlee9823
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
lizamodels9
 
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabiunwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
Abortion pills in Kuwait Cytotec pills in Kuwait
 
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
lizamodels9
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
daisycvs
 

Último (20)

B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxB.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
 
A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMAN
 
Forklift Operations: Safety through Cartoons
Forklift Operations: Safety through CartoonsForklift Operations: Safety through Cartoons
Forklift Operations: Safety through Cartoons
 
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
 
Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Century
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
 
Business Model Canvas (BMC)- A new venture concept
Business Model Canvas (BMC)-  A new venture conceptBusiness Model Canvas (BMC)-  A new venture concept
Business Model Canvas (BMC)- A new venture concept
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfDr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
 
Call Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine ServiceCall Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine Service
 
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
 
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabiunwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
 
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
 
VVVIP Call Girls In Greater Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Greater Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In Greater Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Greater Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
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
  • 4. 3 Why should we mock behaviors?
  • 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
  • 11. Review of the application we are using today 10
  • 12. Exploring different mocking frameworks Moq(Open Source) JustMock (Telerik) Isolator (TypeMock) 11
  • 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
  • 18. 17