SlideShare a Scribd company logo
1 of 26
Microsoft Fakes
Aleksandar Bozinovski
Technical Lead, Seavus
Unit Testing the (almost) Untestable Code
Agenda
Testing in software
Unit testing
• Why unit tests
• Unit tests conventions
• Dependencies, Coupling
Microsoft Fakes
• How does it help
• How does it work
• Stubs
• Shims
• How Do I choose
Q&A
Unit
Testing
Page 5
Strategy Description Visual Studio Tooling
Exploratory
Test
Tester tries to think of possible scenarios not covered by other test strategies and
tests. Useful when engaging users with the testing and observing their (expected)
usage of the system. There are no predefined tests.
Exploratory testing with Microsoft
test Manager (MTM)
Integration
Test
Testing different solution components working together as one. Visual Studio Unit Test features
Load Test Testing under load, in a controlled environment. Visual Studio Load Test Agent
Regression
Test
Regression testing ensures that the system still meets the quality bar after changes
such as bug fixes. Uses a mixture of unit tests and system tests.
Automated testing with MTM
Smoke Test Smoke testing is used to test a new feature or idea before committing the code
changes.
System Test Testing of the entire system against expected features and non-functional
requirements.
Visual Studio Lab Management
Unit Test A test of the smallest unit of code (method / class, and so on) that can be tested
in isolation from other units.
Visual Studio Test Explorer
Unit Test Frameworks
User
Acceptance
Test
Toward the end of the product cycles users are invited to perform acceptance
testing under real-world scenarios, typically based on test cases.
Automated testing with MTM
Testing types
Page 6
Testing types effort
Page 7
Unit testing?
• Unit testing is a software testing method by which individual units of
source code, are tested to determine whether they are fit for use.
• “Units of source code” are methods/functions. The unit test is simply
a method/function that calls another method and determines that
the later works correctly.
• How we can verify that our GetEmployeeById(int id) works correctly? Isolation, a
mechanism that will isolate the code under test, and resolve all dependencies
• Unit tests naming conventions:
• MethodName_StateUnderTest_ExpectedBehavior
• WithdrawMoney_InvalidAccount_ExceptionThrown
• MethodName_ExpectedBehavior_StateUnderTest
• Should_ExpectedBehavior_When_StateUnderTest
• Make sure the code works
• Refactor with confidence
• Support emerging design
• Reducing the cost of fixing bugs by fixing them earlier, rather than later
• Get a quick understanding of the quality of code
Page 8
Why Unit Tests?
Unit tests conventions
• Unit tests naming conventions (there are many)
• MethodName_StateUnderTest_ExpectedBehavior
• WithdrawMoney_InvalidAccount_ReturnsFalse()
• Login_InvalidCredentials_ReturnsErrorMessage()
• MethodName_ExpectedBehavior_StateUnderTest
• WithdrawMoney_ReturnsFalse_InvalidAccount ()
• The 3A of unit testing – Arrange, Act, Assert
• Arrange: Set up the object to be tested. We may need to surround the object with collaborators.
For testing purposes, those collaborators might be test objects (mocks, fakes, etc.) or the real
thing.
• Act: Act on the object (through some mutator). You may need to give it parameters (again,
possibly test objects).
• Assert: Make claims about the object, its collaborators, its parameters, and possibly (rarely!!)
global state.
Page 9
Demo
First unit test
Page 10
• In software engineering, coupling is the manner and degree of
interdependence between software modules. Much of unit tests is
solving dependencies and coupling.
• Practicing unit tests will improve code quality on the long run.
Some principles and patterns must be employed to improve
testability.
• Single responsibility principle (SRP)
• Open Closed Principle (OCP)
• Dependency Inversion Principle (DI)
• Don’t Repeat Yourself (DRY)
• You Aren’t Going to Need It (YAGNI)
Page 11
Dependencies, Coupling
Microsoft
Fakes
• Microsoft Fakes is a framework that enables us to isolate the code we are
testing by replacing dependencies of our code with stubs or shims.
• The Fakes Framework in Visual Studio 2012 is the next generation of Moles
& Stubs. (Read: migrate not upgrade)
• Available in VS 2012/2013 Ultimate
• Works with .Net framework 2.0 and Above
• Newest version available with VS 2015 Enterprise
Page 13
What is the Microsoft Fakes Framework?
• Allows us to quickly implement doubles to support testing in isolation
• Allows us to decouple from slow running dependencies like DB, file system,
message system.
• Decoupling allows us to write order independent unit tests
• Stage data in test methods, not in a DB.
• One unit test failure will not cause a chain reaction
• No need to reset a database to a golden state.
• Allows us to intercept calls to dependencies we do not control.
Page 14
How the Fakes framework helps
• In Microsoft Fakes, the developer must right-click the assembly
reference they wish to mock and select Add Fakes Assembly. This will
generate a new assembly that must be referenced to create the fake
objects.
Page 15
Generated code
Demo
Intro to Stubs
• Should you, or should you not, change the design of
existing code to make it more testable?
Page 17
Question - Code isolation
Demo
Membership Provider
• For every public type in the referenced assembly which are included
into shim-based faking via configuration, the Microsoft Fakes
mechanism generates a shim class. The type name is the same as the
original type, with "Shim" as a prefix.
Page 19
Shims (all instances)
• Shim methods can be injected per instance of an object.
In this example myClass1.MyMethod() will return 5 while
myClass2.MyMethod() will return 10;
Page 20
Shims (one instance)
• Shims must be used within a ShimsContext.Create() using statement.
• If need to execute original code a call to
ShimsContext.ExecuteWithoutShims must be placed inside the
shimmed method.
Page 21
Shims Context and Behavior
Demo
Log Aggregator
Demo
WPF Clock
• Stubs
• Helps if you’re interface-driven
• Creates default() implementations of an interface
• including properties & methods
• Shims
• <magic/>
• Substitute hard-coded types with
*something else* at runtime
Dae Page 24
Stubs and Shims
• Stubs
• If you’ve got interfaces already
• You’re building from scratch
• If you want to save yourself some typing
• You aren’t battling “sealed” and “static” keywords
• Shims
• Stuff is hopelessly stuck together
• Stuff is hopelessly non-testable
• You’re supporting legacy code
• You are Ninja
• Shims are not a long-term solution (except when there is no other
solution).
Page 25
Stubs and Shims
Thank you for your attention!
Copyright: © 2016 Seavus. All rights reserved. | www.seavus.com | www.career.seavus.com

More Related Content

What's hot

Security Code Review 101
Security Code Review 101Security Code Review 101
Security Code Review 101Paul Ionescu
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingSteven Smith
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingSteven Smith
 
Cakephp vs. laravel
Cakephp vs. laravelCakephp vs. laravel
Cakephp vs. laravelGirnarSoft
 
Windows Azure Acid Test
Windows Azure Acid TestWindows Azure Acid Test
Windows Azure Acid Testexpanz
 
Security DevOps - Staying secure in agile projects // OWASP AppSecEU 2015 - A...
Security DevOps - Staying secure in agile projects // OWASP AppSecEU 2015 - A...Security DevOps - Staying secure in agile projects // OWASP AppSecEU 2015 - A...
Security DevOps - Staying secure in agile projects // OWASP AppSecEU 2015 - A...Christian Schneider
 
Test Driven Development using QUnit
Test Driven Development using QUnitTest Driven Development using QUnit
Test Driven Development using QUnitsatejsahu
 
Continuous Security Testing with Devops - OWASP EU 2014
Continuous Security Testing  with Devops - OWASP EU 2014Continuous Security Testing  with Devops - OWASP EU 2014
Continuous Security Testing with Devops - OWASP EU 2014Stephen de Vries
 
Server Side Template Injection by Mandeep Jadon
Server Side Template Injection by Mandeep JadonServer Side Template Injection by Mandeep Jadon
Server Side Template Injection by Mandeep JadonMandeep Jadon
 
SQLi for Security Champions
SQLi for Security ChampionsSQLi for Security Champions
SQLi for Security ChampionsPetraVukmirovic
 
vodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev toolsvodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev toolsvodQA
 
Security Model in .NET Framework
Security Model in .NET FrameworkSecurity Model in .NET Framework
Security Model in .NET FrameworkMikhail Shcherbakov
 
Just Enough Threat Modeling
Just Enough Threat ModelingJust Enough Threat Modeling
Just Enough Threat ModelingStephen de Vries
 
Zane lackey. security at scale. web application security in a continuous depl...
Zane lackey. security at scale. web application security in a continuous depl...Zane lackey. security at scale. web application security in a continuous depl...
Zane lackey. security at scale. web application security in a continuous depl...Yury Chemerkin
 
Refactoring Legacy Web Forms for Test Automation
Refactoring Legacy Web Forms for Test AutomationRefactoring Legacy Web Forms for Test Automation
Refactoring Legacy Web Forms for Test AutomationStephen Fuqua
 
Web Application Security II - SQL Injection
Web Application Security II - SQL InjectionWeb Application Security II - SQL Injection
Web Application Security II - SQL InjectionMd Syed Ahamad
 

What's hot (20)

Security Code Review 101
Security Code Review 101Security Code Review 101
Security Code Review 101
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Security testautomation
Security testautomationSecurity testautomation
Security testautomation
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Cakephp vs. laravel
Cakephp vs. laravelCakephp vs. laravel
Cakephp vs. laravel
 
Windows Azure Acid Test
Windows Azure Acid TestWindows Azure Acid Test
Windows Azure Acid Test
 
Security DevOps - Staying secure in agile projects // OWASP AppSecEU 2015 - A...
Security DevOps - Staying secure in agile projects // OWASP AppSecEU 2015 - A...Security DevOps - Staying secure in agile projects // OWASP AppSecEU 2015 - A...
Security DevOps - Staying secure in agile projects // OWASP AppSecEU 2015 - A...
 
Test Driven Development using QUnit
Test Driven Development using QUnitTest Driven Development using QUnit
Test Driven Development using QUnit
 
Continuous Security Testing with Devops - OWASP EU 2014
Continuous Security Testing  with Devops - OWASP EU 2014Continuous Security Testing  with Devops - OWASP EU 2014
Continuous Security Testing with Devops - OWASP EU 2014
 
Server Side Template Injection by Mandeep Jadon
Server Side Template Injection by Mandeep JadonServer Side Template Injection by Mandeep Jadon
Server Side Template Injection by Mandeep Jadon
 
Javacro 2014 Spring Security 3 Speech
Javacro 2014 Spring Security 3 SpeechJavacro 2014 Spring Security 3 Speech
Javacro 2014 Spring Security 3 Speech
 
SQLi for Security Champions
SQLi for Security ChampionsSQLi for Security Champions
SQLi for Security Champions
 
vodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev toolsvodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev tools
 
Security Model in .NET Framework
Security Model in .NET FrameworkSecurity Model in .NET Framework
Security Model in .NET Framework
 
Presentation
PresentationPresentation
Presentation
 
Practice of AppSec .NET
Practice of AppSec .NETPractice of AppSec .NET
Practice of AppSec .NET
 
Just Enough Threat Modeling
Just Enough Threat ModelingJust Enough Threat Modeling
Just Enough Threat Modeling
 
Zane lackey. security at scale. web application security in a continuous depl...
Zane lackey. security at scale. web application security in a continuous depl...Zane lackey. security at scale. web application security in a continuous depl...
Zane lackey. security at scale. web application security in a continuous depl...
 
Refactoring Legacy Web Forms for Test Automation
Refactoring Legacy Web Forms for Test AutomationRefactoring Legacy Web Forms for Test Automation
Refactoring Legacy Web Forms for Test Automation
 
Web Application Security II - SQL Injection
Web Application Security II - SQL InjectionWeb Application Security II - SQL Injection
Web Application Security II - SQL Injection
 

Viewers also liked

Journal social psychology
Journal social psychologyJournal social psychology
Journal social psychologyKenneth Tan
 
Shipping to russia 1047
Shipping to russia 1047Shipping to russia 1047
Shipping to russia 1047Dean Woodward
 
DAP AN CHI TIET DE THI MB
DAP AN CHI TIET DE THI MBDAP AN CHI TIET DE THI MB
DAP AN CHI TIET DE THI MBdinhnguyenvn
 
Cardiovascular System
Cardiovascular SystemCardiovascular System
Cardiovascular Systemmpattani
 
How to Scale Lead Generation with Facebook, Instagram and Twitter Ads
How to Scale Lead Generation with Facebook, Instagram and Twitter AdsHow to Scale Lead Generation with Facebook, Instagram and Twitter Ads
How to Scale Lead Generation with Facebook, Instagram and Twitter AdsNanigans
 

Viewers also liked (9)

Journal social psychology
Journal social psychologyJournal social psychology
Journal social psychology
 
Carbohydrates
CarbohydratesCarbohydrates
Carbohydrates
 
9 Mehra and Jain JDT
9 Mehra and Jain JDT9 Mehra and Jain JDT
9 Mehra and Jain JDT
 
Repowering America
Repowering AmericaRepowering America
Repowering America
 
Shipping to russia 1047
Shipping to russia 1047Shipping to russia 1047
Shipping to russia 1047
 
CTB Resume
CTB ResumeCTB Resume
CTB Resume
 
DAP AN CHI TIET DE THI MB
DAP AN CHI TIET DE THI MBDAP AN CHI TIET DE THI MB
DAP AN CHI TIET DE THI MB
 
Cardiovascular System
Cardiovascular SystemCardiovascular System
Cardiovascular System
 
How to Scale Lead Generation with Facebook, Instagram and Twitter Ads
How to Scale Lead Generation with Facebook, Instagram and Twitter AdsHow to Scale Lead Generation with Facebook, Instagram and Twitter Ads
How to Scale Lead Generation with Facebook, Instagram and Twitter Ads
 

Similar to Unit Tests with Microsoft Fakes

Automated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsAutomated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsQuontra Solutions
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing FundamentalsRichard Paul
 
Testing Frameworks
Testing FrameworksTesting Frameworks
Testing FrameworksMoataz Nabil
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDDDror Helper
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuPhat VU
 
Advanced Coded UI Testing
Advanced Coded UI TestingAdvanced Coded UI Testing
Advanced Coded UI TestingShai Raiten
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...AgileNetwork
 
Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...DevDay.org
 
Tools for Software Testing
Tools for Software TestingTools for Software Testing
Tools for Software TestingMohammed Moishin
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingSahar Nofal
 
Beginners overview of automated testing with Rspec
Beginners overview of automated testing with RspecBeginners overview of automated testing with Rspec
Beginners overview of automated testing with Rspecjeffrey1ross
 
Cloud-based Test Microservices JavaOne 2014
Cloud-based Test Microservices JavaOne 2014Cloud-based Test Microservices JavaOne 2014
Cloud-based Test Microservices JavaOne 2014Shelley Lambert
 

Similar to Unit Tests with Microsoft Fakes (20)

Automated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsAutomated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra Solutions
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
Testing Angular
Testing AngularTesting Angular
Testing Angular
 
Testing Frameworks
Testing FrameworksTesting Frameworks
Testing Frameworks
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvu
 
Advanced Coded UI Testing
Advanced Coded UI TestingAdvanced Coded UI Testing
Advanced Coded UI Testing
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
 
Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code Testing
 
NET Code Testing
NET Code TestingNET Code Testing
NET Code Testing
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
 
Tools for Software Testing
Tools for Software TestingTools for Software Testing
Tools for Software Testing
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
AspectMock
AspectMockAspectMock
AspectMock
 
Beginners overview of automated testing with Rspec
Beginners overview of automated testing with RspecBeginners overview of automated testing with Rspec
Beginners overview of automated testing with Rspec
 
Cloud-based Test Microservices JavaOne 2014
Cloud-based Test Microservices JavaOne 2014Cloud-based Test Microservices JavaOne 2014
Cloud-based Test Microservices JavaOne 2014
 

Recently uploaded

Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 

Recently uploaded (20)

Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 

Unit Tests with Microsoft Fakes

  • 1.
  • 2. Microsoft Fakes Aleksandar Bozinovski Technical Lead, Seavus Unit Testing the (almost) Untestable Code
  • 3. Agenda Testing in software Unit testing • Why unit tests • Unit tests conventions • Dependencies, Coupling Microsoft Fakes • How does it help • How does it work • Stubs • Shims • How Do I choose Q&A
  • 5. Page 5 Strategy Description Visual Studio Tooling Exploratory Test Tester tries to think of possible scenarios not covered by other test strategies and tests. Useful when engaging users with the testing and observing their (expected) usage of the system. There are no predefined tests. Exploratory testing with Microsoft test Manager (MTM) Integration Test Testing different solution components working together as one. Visual Studio Unit Test features Load Test Testing under load, in a controlled environment. Visual Studio Load Test Agent Regression Test Regression testing ensures that the system still meets the quality bar after changes such as bug fixes. Uses a mixture of unit tests and system tests. Automated testing with MTM Smoke Test Smoke testing is used to test a new feature or idea before committing the code changes. System Test Testing of the entire system against expected features and non-functional requirements. Visual Studio Lab Management Unit Test A test of the smallest unit of code (method / class, and so on) that can be tested in isolation from other units. Visual Studio Test Explorer Unit Test Frameworks User Acceptance Test Toward the end of the product cycles users are invited to perform acceptance testing under real-world scenarios, typically based on test cases. Automated testing with MTM Testing types
  • 7. Page 7 Unit testing? • Unit testing is a software testing method by which individual units of source code, are tested to determine whether they are fit for use. • “Units of source code” are methods/functions. The unit test is simply a method/function that calls another method and determines that the later works correctly. • How we can verify that our GetEmployeeById(int id) works correctly? Isolation, a mechanism that will isolate the code under test, and resolve all dependencies • Unit tests naming conventions: • MethodName_StateUnderTest_ExpectedBehavior • WithdrawMoney_InvalidAccount_ExceptionThrown • MethodName_ExpectedBehavior_StateUnderTest • Should_ExpectedBehavior_When_StateUnderTest
  • 8. • Make sure the code works • Refactor with confidence • Support emerging design • Reducing the cost of fixing bugs by fixing them earlier, rather than later • Get a quick understanding of the quality of code Page 8 Why Unit Tests?
  • 9. Unit tests conventions • Unit tests naming conventions (there are many) • MethodName_StateUnderTest_ExpectedBehavior • WithdrawMoney_InvalidAccount_ReturnsFalse() • Login_InvalidCredentials_ReturnsErrorMessage() • MethodName_ExpectedBehavior_StateUnderTest • WithdrawMoney_ReturnsFalse_InvalidAccount () • The 3A of unit testing – Arrange, Act, Assert • Arrange: Set up the object to be tested. We may need to surround the object with collaborators. For testing purposes, those collaborators might be test objects (mocks, fakes, etc.) or the real thing. • Act: Act on the object (through some mutator). You may need to give it parameters (again, possibly test objects). • Assert: Make claims about the object, its collaborators, its parameters, and possibly (rarely!!) global state. Page 9
  • 11. • In software engineering, coupling is the manner and degree of interdependence between software modules. Much of unit tests is solving dependencies and coupling. • Practicing unit tests will improve code quality on the long run. Some principles and patterns must be employed to improve testability. • Single responsibility principle (SRP) • Open Closed Principle (OCP) • Dependency Inversion Principle (DI) • Don’t Repeat Yourself (DRY) • You Aren’t Going to Need It (YAGNI) Page 11 Dependencies, Coupling
  • 13. • Microsoft Fakes is a framework that enables us to isolate the code we are testing by replacing dependencies of our code with stubs or shims. • The Fakes Framework in Visual Studio 2012 is the next generation of Moles & Stubs. (Read: migrate not upgrade) • Available in VS 2012/2013 Ultimate • Works with .Net framework 2.0 and Above • Newest version available with VS 2015 Enterprise Page 13 What is the Microsoft Fakes Framework?
  • 14. • Allows us to quickly implement doubles to support testing in isolation • Allows us to decouple from slow running dependencies like DB, file system, message system. • Decoupling allows us to write order independent unit tests • Stage data in test methods, not in a DB. • One unit test failure will not cause a chain reaction • No need to reset a database to a golden state. • Allows us to intercept calls to dependencies we do not control. Page 14 How the Fakes framework helps
  • 15. • In Microsoft Fakes, the developer must right-click the assembly reference they wish to mock and select Add Fakes Assembly. This will generate a new assembly that must be referenced to create the fake objects. Page 15 Generated code
  • 17. • Should you, or should you not, change the design of existing code to make it more testable? Page 17 Question - Code isolation
  • 19. • For every public type in the referenced assembly which are included into shim-based faking via configuration, the Microsoft Fakes mechanism generates a shim class. The type name is the same as the original type, with "Shim" as a prefix. Page 19 Shims (all instances)
  • 20. • Shim methods can be injected per instance of an object. In this example myClass1.MyMethod() will return 5 while myClass2.MyMethod() will return 10; Page 20 Shims (one instance)
  • 21. • Shims must be used within a ShimsContext.Create() using statement. • If need to execute original code a call to ShimsContext.ExecuteWithoutShims must be placed inside the shimmed method. Page 21 Shims Context and Behavior
  • 24. • Stubs • Helps if you’re interface-driven • Creates default() implementations of an interface • including properties & methods • Shims • <magic/> • Substitute hard-coded types with *something else* at runtime Dae Page 24 Stubs and Shims
  • 25. • Stubs • If you’ve got interfaces already • You’re building from scratch • If you want to save yourself some typing • You aren’t battling “sealed” and “static” keywords • Shims • Stuff is hopelessly stuck together • Stuff is hopelessly non-testable • You’re supporting legacy code • You are Ninja • Shims are not a long-term solution (except when there is no other solution). Page 25 Stubs and Shims
  • 26. Thank you for your attention! Copyright: © 2016 Seavus. All rights reserved. | www.seavus.com | www.career.seavus.com

Editor's Notes

  1. MY Practical definitions SRP – designing classes so they do one thing well keeps the class simple. Simple classes are easier to test. Code smell large classes and large methods. OCP – Design classes to be resilient in the face of change. For example using abstract methods on base classes lets add behavior without changing existing code. Code smell frequently adding a case to a switch statement or If then Else If DI - high level classes are not working directly with low level classes, they are using interfaces as an abstract layer. Why – Easier to test in isolation! Code smell : “Newing up classes” DRY – Don’t repeat yourself – code and test it once reuse many times. Less code to write – less code to change (1 spot) YAGNI – Don’t write code until you need it. Remove unused code and corresponding tests – greatest refactor you do removes code.