SlideShare uma empresa Scribd logo
1 de 17
TypeMock Isolator
Mocks/Fakes/Stubs
By Brandon D’Imperio
ImaginaryDevelopment@gmail.com
Testing is hard!
• Testing is too much work
• I can’t get my developers to write tests
• I can’t get my teammates to write tests
• My codebase was not written with testability
• Mocking frameworks take too much work to
fake out deep call chains
• My dependencies are static, sealed, or non-
public and can’t be faked
Jargon/Definitions
• Unit Test
– Code (usually a method) that runs another piece of code (usually a single method/function in
isolation) and checks the correctness of an assumption afterward. If the assumptions are
wrong, the unit test is failed.
• Fake
– Encapsulates noth Stubs and Mocks
• Stub
– A controllable replacement for an existing dependency in the system.
– Use a stub to test your code without dealing with the dependency directly
– Can not fail a test
– Usually just a dummy object that does nothing, returns property values as instructed
– Not the item being tested again
• Mock
– A fake object in the system that decides whether the unit test has passed or failed.
– Does so by verifying whether the object under test interacted as expected with the Fake
Testing Types
Testing
• User Model Testing - link
• Acceptance Testing - link
• Usability Testing
• Performance and load testing
• Security Testing
• Integration Testing
• Unit Testing – uses fakes to isolate
Unit Testing weaknesses
• Can not catch integration errors, system-level
errors, or non-functional (performance,
security, etc)
• Additional maintenance
• Additional coding
• Unit tests may share the same blind spots as
the code because they are written by the
same developer
Unit Testing strengths
• A code driven documentation
– Less likely to get out of sync than comments
– Less likely to get out of sync than documentation
• Regression reduction
• Instant feedback
• Safer refactoring and feature addition
• Unit tests may help the developer find blind
spots
What is a good unit test?
• Automated/repeatable
• Easy to implement
• Once written, remains for future use/versions
• Anyone should be able to run it
• Runs quickly (up to 20,000 in a minute)
– No dependencies
What’s a mock/isolation framework?
• In order to properly unit test your external
dependencies must be replaced. A unit test
should ONLY fail because of the code you are
testing, otherwise it’s an integration test.
• A mock framework is a tool to replace external
dependencies with fakes.
What does TypeMock bring?
• Fake static/sealed/non-public elements
– DateTime.Now faking!
• No design for testability required
• Recursive fakes (nested call chains) a.b.c.d
Code!
• Let’s look at some actual code
TypeMock vs Others
• Some mock frameworks can only mock
interfaces
• nUnit.Mocks – no events or generics mocking
• Rhino Mocks – no strings for method names,
single developer (if he stops, game over?)
– Documentation does not appear to support non-
interface mocking
• nMock 2.0 – currently in RC status, appears to
require interfaces to fake
Debate
• Advocates of Testability say you don’t get the
clean SOLID design of writing for testability
• Should the isolation framework be dictating
design decisions?
• Design for testability leaves out the majority
of work… Brownfield applications
TDD and indirectly unit testing
research
• "We found that test-first students on average wrote more tests and,
in turn, students who wrote more tests tended to be more
productive. We also observed that the minimum quality increased
linearly with the number of programmer tests, independent of the
development strategy employed."
• "The external validity of the results could be limited since the
subjects were students. Runeson [21] compared freshmen,
graduate, and professional developers and concluded that similar
improvement trends persisted among the three groups. Replicated
experiments by Porter
and Votta [22] and Höst et al. [23] suggest that students may
provide an adequate model of the professional population."
• Taken from
http://weblogs.asp.net/rosherove/archive/2008/01/25/research-
finds-tdd-boosts-developer-productivity.aspx
More reading…
• Martin Fowler – Mocks aren’t stubs
• Uncle Bob – TDD Derangement Syndrome
Good Practices
• Only a single Assert per test
• Code a test for both the Happy Path and the
Unhappy Path
• Start out testing by hitting your longest, most
cyclomatically complex, most critical, or just
hardest to understand methods.
• Try to reduce the external dependency contact
points in a class
• Write a test that FAILS first! Write the test before
the code, or comment out the part that should
make it succeed.
My Passions
• Tooling anything that makes coding easier or
more productive
• MVC
• Extensibility

Mais conteúdo relacionado

Mais procurados

Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
Foyzul Karim
 
Moq presentation
Moq presentationMoq presentation
Moq presentation
LynxStar
 
An Overview of automated testing (1)
An Overview of automated testing (1)An Overview of automated testing (1)
An Overview of automated testing (1)
Rodrigo Lopes
 
The problem with tdd
The problem with tddThe problem with tdd
The problem with tdd
Dror Helper
 

Mais procurados (20)

Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
 
Mixing testing types to improve your testing results
Mixing testing types to improve your testing resultsMixing testing types to improve your testing results
Mixing testing types to improve your testing results
 
Moq presentation
Moq presentationMoq presentation
Moq presentation
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Code Review Matters and Manners
Code Review Matters and MannersCode Review Matters and Manners
Code Review Matters and Manners
 
Test-driven development with Node.js
Test-driven development with Node.jsTest-driven development with Node.js
Test-driven development with Node.js
 
Automated Testing but like for PowerShell (April 2012)
Automated Testing but like for PowerShell (April 2012)Automated Testing but like for PowerShell (April 2012)
Automated Testing but like for PowerShell (April 2012)
 
Unit Testing talk
Unit Testing talkUnit Testing talk
Unit Testing talk
 
Writing Better Tests - Applying Clean-Code TDD at 99designs
Writing Better Tests - Applying Clean-Code TDD at 99designsWriting Better Tests - Applying Clean-Code TDD at 99designs
Writing Better Tests - Applying Clean-Code TDD at 99designs
 
Level Up Your Automated Tests
Level Up Your Automated TestsLevel Up Your Automated Tests
Level Up Your Automated Tests
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
 
Unit testing
Unit testingUnit testing
Unit testing
 
An Overview of automated testing (1)
An Overview of automated testing (1)An Overview of automated testing (1)
An Overview of automated testing (1)
 
How to get the most out of code reviews
How to get the most out of code reviewsHow to get the most out of code reviews
How to get the most out of code reviews
 
Unit testing in PHP
Unit testing in PHPUnit testing in PHP
Unit testing in PHP
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
 
Introduction to test programming
Introduction to test programmingIntroduction to test programming
Introduction to test programming
 
The problem with tdd
The problem with tddThe problem with tdd
The problem with tdd
 
Exploratory testing
Exploratory testingExploratory testing
Exploratory testing
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 

Semelhante a Type mock isolator

Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Ortus Solutions, Corp
 
Introduction to-automated-testing
Introduction to-automated-testingIntroduction to-automated-testing
Introduction to-automated-testing
BestBrains
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
Meilan Ou
 
Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven development
Einar Ingebrigtsen
 

Semelhante a Type mock isolator (20)

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...
 
CNUG TDD June 2014
CNUG TDD June 2014CNUG TDD June 2014
CNUG TDD June 2014
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
 
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
 
Software testing
Software testingSoftware testing
Software testing
 
Eurosport's Kodakademi #2
Eurosport's Kodakademi #2Eurosport's Kodakademi #2
Eurosport's Kodakademi #2
 
Presentation delex
Presentation delexPresentation delex
Presentation delex
 
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Introduction to Automated Testing
Introduction to Automated TestingIntroduction to Automated Testing
Introduction to Automated Testing
 
Introduction to-automated-testing
Introduction to-automated-testingIntroduction to-automated-testing
Introduction to-automated-testing
 
Software testing part
Software testing partSoftware testing part
Software testing part
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Week 14 Unit Testing.pptx
Week 14  Unit Testing.pptxWeek 14  Unit Testing.pptx
Week 14 Unit Testing.pptx
 
An Introduction To Software Development - Final Review
An Introduction To Software Development - Final ReviewAn Introduction To Software Development - Final Review
An Introduction To Software Development - Final Review
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)
 
Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven development
 
Practical unit testing in c & c++
Practical unit testing in c & c++Practical unit testing in c & c++
Practical unit testing in c & c++
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud Shaon
 

Mais de MaslowB (8)

F# for BLOBA, by brandon d'imperio
F# for BLOBA, by brandon d'imperioF# for BLOBA, by brandon d'imperio
F# for BLOBA, by brandon d'imperio
 
Knockout vs. angular
Knockout vs. angularKnockout vs. angular
Knockout vs. angular
 
Js testing
Js testingJs testing
Js testing
 
What’s new mvc 4
What’s new mvc 4What’s new mvc 4
What’s new mvc 4
 
A clean repository pattern in ef
A clean repository pattern in efA clean repository pattern in ef
A clean repository pattern in ef
 
Metrics
MetricsMetrics
Metrics
 
Mvc presentation
Mvc presentationMvc presentation
Mvc presentation
 
Metaprogramming by brandon
Metaprogramming by brandonMetaprogramming by brandon
Metaprogramming by brandon
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Type mock isolator

  • 1. TypeMock Isolator Mocks/Fakes/Stubs By Brandon D’Imperio ImaginaryDevelopment@gmail.com
  • 2. Testing is hard! • Testing is too much work • I can’t get my developers to write tests • I can’t get my teammates to write tests • My codebase was not written with testability • Mocking frameworks take too much work to fake out deep call chains • My dependencies are static, sealed, or non- public and can’t be faked
  • 3. Jargon/Definitions • Unit Test – Code (usually a method) that runs another piece of code (usually a single method/function in isolation) and checks the correctness of an assumption afterward. If the assumptions are wrong, the unit test is failed. • Fake – Encapsulates noth Stubs and Mocks • Stub – A controllable replacement for an existing dependency in the system. – Use a stub to test your code without dealing with the dependency directly – Can not fail a test – Usually just a dummy object that does nothing, returns property values as instructed – Not the item being tested again • Mock – A fake object in the system that decides whether the unit test has passed or failed. – Does so by verifying whether the object under test interacted as expected with the Fake
  • 5. Testing • User Model Testing - link • Acceptance Testing - link • Usability Testing • Performance and load testing • Security Testing • Integration Testing • Unit Testing – uses fakes to isolate
  • 6. Unit Testing weaknesses • Can not catch integration errors, system-level errors, or non-functional (performance, security, etc) • Additional maintenance • Additional coding • Unit tests may share the same blind spots as the code because they are written by the same developer
  • 7. Unit Testing strengths • A code driven documentation – Less likely to get out of sync than comments – Less likely to get out of sync than documentation • Regression reduction • Instant feedback • Safer refactoring and feature addition • Unit tests may help the developer find blind spots
  • 8. What is a good unit test? • Automated/repeatable • Easy to implement • Once written, remains for future use/versions • Anyone should be able to run it • Runs quickly (up to 20,000 in a minute) – No dependencies
  • 9. What’s a mock/isolation framework? • In order to properly unit test your external dependencies must be replaced. A unit test should ONLY fail because of the code you are testing, otherwise it’s an integration test. • A mock framework is a tool to replace external dependencies with fakes.
  • 10. What does TypeMock bring? • Fake static/sealed/non-public elements – DateTime.Now faking! • No design for testability required • Recursive fakes (nested call chains) a.b.c.d
  • 11. Code! • Let’s look at some actual code
  • 12. TypeMock vs Others • Some mock frameworks can only mock interfaces • nUnit.Mocks – no events or generics mocking • Rhino Mocks – no strings for method names, single developer (if he stops, game over?) – Documentation does not appear to support non- interface mocking • nMock 2.0 – currently in RC status, appears to require interfaces to fake
  • 13. Debate • Advocates of Testability say you don’t get the clean SOLID design of writing for testability • Should the isolation framework be dictating design decisions? • Design for testability leaves out the majority of work… Brownfield applications
  • 14. TDD and indirectly unit testing research • "We found that test-first students on average wrote more tests and, in turn, students who wrote more tests tended to be more productive. We also observed that the minimum quality increased linearly with the number of programmer tests, independent of the development strategy employed." • "The external validity of the results could be limited since the subjects were students. Runeson [21] compared freshmen, graduate, and professional developers and concluded that similar improvement trends persisted among the three groups. Replicated experiments by Porter and Votta [22] and Höst et al. [23] suggest that students may provide an adequate model of the professional population." • Taken from http://weblogs.asp.net/rosherove/archive/2008/01/25/research- finds-tdd-boosts-developer-productivity.aspx
  • 15. More reading… • Martin Fowler – Mocks aren’t stubs • Uncle Bob – TDD Derangement Syndrome
  • 16. Good Practices • Only a single Assert per test • Code a test for both the Happy Path and the Unhappy Path • Start out testing by hitting your longest, most cyclomatically complex, most critical, or just hardest to understand methods. • Try to reduce the external dependency contact points in a class • Write a test that FAILS first! Write the test before the code, or comment out the part that should make it succeed.
  • 17. My Passions • Tooling anything that makes coding easier or more productive • MVC • Extensibility

Notas do Editor

  1. Reference: http://weblogs.asp.net/rosherove/archive/2007/04/26/choosing-a-mock-object-framework.aspxNmock: http://www.nmock.org/cheatsheet.html