Triumph Over Legacy Code with Unit Testing

Typemock
TypemockTypemock
Testing Legacy Code
Exciting approach to unit testing any code,
Keeping high quality & agility, and
Creating wicked applications
Eli Lopian,
Founder Typemock
WHAT
YOU
WILL
LEARN
• Problems with legacy code
• How isolation helps
• How to unit test legacy code (demo)
Legacy code is… Code without tests
Michael Feathers
Developers write millions of lines
of legacy code
every day!
Legacy Code Problems
• Code is tangled
• Complex Tests
• Death spiral
• Catch-22
Isolate it… (The most effective way to test legacy code )
• Fake Dependencies
• Change Code Behavior
• Verify Calls
Fake Dependencies
var fakeProcess = Isolate.Fake.Instance<Process>();
fakeProcess.MainModule.Site.Name
var fakeProcessHandle = Isolate.Fake.NextInstance<Process>();
var fakeAllProcessHandle = Isolate.Fake.AllInstances<Process>();
var fakeSimulator = Isolate.Fake.Dependencies<Simulator>();
Create a fake instance of a type:1
Signal that the next instance will be faked:2
Fake ALL the dependencies that are passed through the constructor:3
Change Code Behavior
Isolate.WhenCalled(() => DateTime.Now).WillReturn(new DateTime(1977,5,25));
Isolate.WhenCalled(() => fake.MainModule.Site.Name).WillReturn("Typemock rocks");
var counter = new Counter ();
Isolate.WhenCalled(() => counter.Increment()).WillReturn(1);
Isolate.WhenCalled(() => counter.Increment()).WillReturn(2);
Change return value of a static method:1
Change return value of a chain of calls:
Change return value of a method from a real object:
2
3
Verify Calls
Isolate.Verify.WasCalledWithAnyArguments(() => backoffice.Save());
Isolate.Verify.WasNotCalled(() => backoffice.Save());
int count = Isolate.Verify.GetTimesCalled(() => backoffice.Save());
Assert.AreEqual(2, count);
The code called Save():
The code never calls Save():
The code calls the method 2 times:
1
2
3
Triumph Over Legacy Code with Unit Testing
Cannot call constructor because of dependencies
Need method to return fake value
Nothing to assert
Fake an object to change its behavior.
Need to change behavior of a private method
Need to Fake an object that is created in the code
• Isolate Dependencies
• Control Behavior
• Assert Calls
Fake It Till You Make It!
Powerful
Mocking
Visual
CoverageSmartRunner
Typemock Isolator
Productivity*
* Complete Edition Only
• Trial License
• Online webinars
• Cookbook
• Team Demonstration
You can get more resources
www.typemock.com
blog.typemock.com
@typemock
eli@typemock.com
Questions?
Image credits
flickr@33909700@N02
flickr@jamesuk
flickr@nikio
flickr@duchamp
flickr@kalyan02
flickr@clarkstonscamp
flicker@95786359@N05
1 of 20

Recommended

Owasp tds by
Owasp tdsOwasp tds
Owasp tdssnyff
647 views36 slides
Test-Driven Sitecore by
Test-Driven SitecoreTest-Driven Sitecore
Test-Driven SitecoreCaitlin Portrie
777 views36 slides
.NET Unit Testing w/ Microsoft Fakes (TechCamp Memphis Spring 2012) by
.NET Unit Testing w/ Microsoft Fakes (TechCamp Memphis Spring 2012).NET Unit Testing w/ Microsoft Fakes (TechCamp Memphis Spring 2012)
.NET Unit Testing w/ Microsoft Fakes (TechCamp Memphis Spring 2012)lancehilliard
951 views8 slides
Ln monitoring repositories by
Ln monitoring repositoriesLn monitoring repositories
Ln monitoring repositoriessnyff
989 views50 slides
Android Deobfuscation: Tools and Techniques by
Android Deobfuscation: Tools and TechniquesAndroid Deobfuscation: Tools and Techniques
Android Deobfuscation: Tools and Techniquescaleb194331
502 views35 slides
Android Malware and Machine Learning by
Android Malware and Machine LearningAndroid Malware and Machine Learning
Android Malware and Machine Learningcaleb194331
115 views29 slides

More Related Content

Similar to Triumph Over Legacy Code with Unit Testing

Summit 16: Stop Writing Legacy Code! by
Summit 16: Stop Writing Legacy Code!Summit 16: Stop Writing Legacy Code!
Summit 16: Stop Writing Legacy Code!OPNFV
376 views19 slides
Elixir Programming Language 101 by
Elixir Programming Language 101Elixir Programming Language 101
Elixir Programming Language 101Around25
270 views35 slides
libinjection: from SQLi to XSS  by Nick Galbreath by
libinjection: from SQLi to XSS  by Nick Galbreathlibinjection: from SQLi to XSS  by Nick Galbreath
libinjection: from SQLi to XSS  by Nick GalbreathCODE BLUE
6.8K views38 slides
Workshop fight legacy code write unit test by
Workshop fight legacy code write unit testWorkshop fight legacy code write unit test
Workshop fight legacy code write unit testTung Nguyen Thanh
576 views35 slides
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015] by
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015][XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]Agile đây Vietnam
557 views35 slides
Integreation by
IntegreationIntegreation
IntegreationSean Killeen
1.6K views55 slides

Similar to Triumph Over Legacy Code with Unit Testing (20)

Summit 16: Stop Writing Legacy Code! by OPNFV
Summit 16: Stop Writing Legacy Code!Summit 16: Stop Writing Legacy Code!
Summit 16: Stop Writing Legacy Code!
OPNFV376 views
Elixir Programming Language 101 by Around25
Elixir Programming Language 101Elixir Programming Language 101
Elixir Programming Language 101
Around25270 views
libinjection: from SQLi to XSS  by Nick Galbreath by CODE BLUE
libinjection: from SQLi to XSS  by Nick Galbreathlibinjection: from SQLi to XSS  by Nick Galbreath
libinjection: from SQLi to XSS  by Nick Galbreath
CODE BLUE6.8K views
Workshop fight legacy code write unit test by Tung Nguyen Thanh
Workshop fight legacy code write unit testWorkshop fight legacy code write unit test
Workshop fight legacy code write unit test
Tung Nguyen Thanh576 views
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015] by Agile đây Vietnam
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015][XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
Finding Needles in Haystacks by snyff
Finding Needles in HaystacksFinding Needles in Haystacks
Finding Needles in Haystacks
snyff4.3K views
Real life unit testing tools and practices by Gil Zilberfeld
Real life unit testing   tools and practicesReal life unit testing   tools and practices
Real life unit testing tools and practices
Gil Zilberfeld481 views
Testing, Learning and Professionalism — 20171214 by David Rodenas
Testing, Learning and Professionalism — 20171214Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214
David Rodenas155 views
Building unit tests correctly by Dror Helper
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
Dror Helper1.4K views
Working with Legacy Code by Eyal Golan
Working with Legacy CodeWorking with Legacy Code
Working with Legacy Code
Eyal Golan5.8K views
Let's Talk Technical: Malware Evasion and Detection by James Haughom Jr
Let's Talk Technical: Malware Evasion and DetectionLet's Talk Technical: Malware Evasion and Detection
Let's Talk Technical: Malware Evasion and Detection
James Haughom Jr584 views
An Introduction to Unit Testing by Joe Tremblay
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
Joe Tremblay3.6K views
DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo... by Felipe Prado
DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...
DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...
Felipe Prado70 views
Developer Job in Practice by intive
Developer Job in PracticeDeveloper Job in Practice
Developer Job in Practice
intive835 views
Breaking Dependencies to Allow Unit Testing - DevIntersection Spring 2016 by Steven Smith
Breaking Dependencies to Allow Unit Testing - DevIntersection Spring 2016Breaking Dependencies to Allow Unit Testing - DevIntersection Spring 2016
Breaking Dependencies to Allow Unit Testing - DevIntersection Spring 2016
Steven Smith1.4K views
Mocking vtcc3 - en by vgrondin
Mocking   vtcc3 - enMocking   vtcc3 - en
Mocking vtcc3 - en
vgrondin1.3K views
VoltDB and Erlang - Tech planet 2012 by Eonblast
VoltDB and Erlang - Tech planet 2012VoltDB and Erlang - Tech planet 2012
VoltDB and Erlang - Tech planet 2012
Eonblast4.4K views

Recently uploaded

Future of Learning - Yap Aye Wee.pdf by
Future of Learning - Yap Aye Wee.pdfFuture of Learning - Yap Aye Wee.pdf
Future of Learning - Yap Aye Wee.pdfNUS-ISS
41 views11 slides
Top 10 Strategic Technologies in 2024: AI and Automation by
Top 10 Strategic Technologies in 2024: AI and AutomationTop 10 Strategic Technologies in 2024: AI and Automation
Top 10 Strategic Technologies in 2024: AI and AutomationAutomationEdge Technologies
14 views14 slides
Report 2030 Digital Decade by
Report 2030 Digital DecadeReport 2030 Digital Decade
Report 2030 Digital DecadeMassimo Talia
14 views41 slides
RADIUS-Omnichannel Interaction System by
RADIUS-Omnichannel Interaction SystemRADIUS-Omnichannel Interaction System
RADIUS-Omnichannel Interaction SystemRADIUS
15 views21 slides
Attacking IoT Devices from a Web Perspective - Linux Day by
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day Simone Onofri
15 views68 slides

Recently uploaded(20)

Future of Learning - Yap Aye Wee.pdf by NUS-ISS
Future of Learning - Yap Aye Wee.pdfFuture of Learning - Yap Aye Wee.pdf
Future of Learning - Yap Aye Wee.pdf
NUS-ISS41 views
RADIUS-Omnichannel Interaction System by RADIUS
RADIUS-Omnichannel Interaction SystemRADIUS-Omnichannel Interaction System
RADIUS-Omnichannel Interaction System
RADIUS15 views
Attacking IoT Devices from a Web Perspective - Linux Day by Simone Onofri
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day
Simone Onofri15 views
Spesifikasi Lengkap ASUS Vivobook Go 14 by Dot Semarang
Spesifikasi Lengkap ASUS Vivobook Go 14Spesifikasi Lengkap ASUS Vivobook Go 14
Spesifikasi Lengkap ASUS Vivobook Go 14
Dot Semarang35 views
AI: mind, matter, meaning, metaphors, being, becoming, life values by Twain Liu 刘秋艳
AI: mind, matter, meaning, metaphors, being, becoming, life valuesAI: mind, matter, meaning, metaphors, being, becoming, life values
AI: mind, matter, meaning, metaphors, being, becoming, life values
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor... by Vadym Kazulkin
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
Vadym Kazulkin75 views
Web Dev - 1 PPT.pdf by gdsczhcet
Web Dev - 1 PPT.pdfWeb Dev - 1 PPT.pdf
Web Dev - 1 PPT.pdf
gdsczhcet55 views
STPI OctaNE CoE Brochure.pdf by madhurjyapb
STPI OctaNE CoE Brochure.pdfSTPI OctaNE CoE Brochure.pdf
STPI OctaNE CoE Brochure.pdf
madhurjyapb12 views
Business Analyst Series 2023 - Week 3 Session 5 by DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10209 views
Black and White Modern Science Presentation.pptx by maryamkhalid2916
Black and White Modern Science Presentation.pptxBlack and White Modern Science Presentation.pptx
Black and White Modern Science Presentation.pptx
maryamkhalid291614 views
[2023] Putting the R! in R&D.pdf by Eleanor McHugh
[2023] Putting the R! in R&D.pdf[2023] Putting the R! in R&D.pdf
[2023] Putting the R! in R&D.pdf
Eleanor McHugh38 views
Perth MeetUp November 2023 by Michael Price
Perth MeetUp November 2023 Perth MeetUp November 2023
Perth MeetUp November 2023
Michael Price15 views
The details of description: Techniques, tips, and tangents on alternative tex... by BookNet Canada
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...
BookNet Canada121 views
Five Things You SHOULD Know About Postman by Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman27 views
PharoJS - Zürich Smalltalk Group Meetup November 2023 by Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi120 views

Triumph Over Legacy Code with Unit Testing