Writing Unit Tests with Microsoft Fakes

Report
Writing Unit Tests with Microsoft Fakes
Writing Unit Tests with Microsoft Fakes
Tim Star
[email protected]
http://www.intertech.com/Blog/
http://timsbrownbaglunch.blogspot.com/ /
A Consulting Division Presentation
Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 1
Month Date, 2006 — © — All Rights Reserved
Writing Unit Tests with Microsoft Fakes
Experience
•
•
•
•
•
•
•
•
•
•
Principal Consultant / .Net Architect
Visual Studio ALM Ranger – 5 years
MS Testing tools consulting and training
Load testing
Development Manager
MCPD Enterprise App Development
3.5/4.0 Framework
MCTS WPF/TFS
MCP – VS ALM and Testing with MTM
MCT
4 X Microsoft MVP
Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 2
Writing Unit Tests with Microsoft Fakes
Agenda
• Unit testing Why
• Design Principles for testability
• What is the Fakes framework
•
•
•
•
•
How does it help
How does it work
Stubs
Shims
How Do I choose
• Resources
• Demo
Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 3
Writing Unit Tests with Microsoft Fakes
Why Unit test
• Make sure the code works
• Refactor with confidence
• Support emerging design
• Find errors and design flaws early - $
• Get a quick understanding of the quality of code
Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 4
Writing Unit Tests with Microsoft Fakes
Design Principles for testability
•
•
•
•
•
•
•
Single responsibility principle (SRP)
Open Closed Principle (OCP)
Liskov Substitution Principle (LSP)
Interface Segregation Principle (ISP)
Dependency Inversion Principle (DIP)
Don’t Repeat Yourself (DRY)
You Aren’t Going to Need It (YAGNI)
• Great resource: “Agile Principles, Patterns, and Practices in
C#”
Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 5
Writing Unit Tests with Microsoft Fakes
What is the Microsoft Fakes
Framework?
• 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 Ultimate or VS 2012 Premium beginning with
Update 2
• Works with .Net framework 2.0 and Above
Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 6
Writing Unit Tests with Microsoft Fakes
How does the Fakes framework Help
• 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.
Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 7
Writing Unit Tests with Microsoft Fakes
Stubs and Shims
• Stubs – Code generator that generates classes that implement
interfaces. Uses delegates so our tests supply the implementation.
• Shims – Modifies code at runtime to intercept calls to
dependencies. (Read: slower) Accepts delegates so tests can
provide alternate implementation.
Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 8
Writing Unit Tests with Microsoft Fakes
Choosing between stubs and shims
•
Performance. Shims run slower because they rewrite your code at run time. Stubs do not have
this performance overhead and are as fast as virtual methods can go.
•
Static methods, sealed types. You can only use stubs to implement interfaces. Therefore, stub
types cannot be used for static methods, non-virtual methods, sealed virtual methods,
methods in sealed types, and so on.
•
Internal types. Both stubs and shims can be used with internal types that are made
accessible by using the assembly attribute InternalsVisibleToAttribute.
•
Private methods. Shims can replace calls to private methods if all the types on the method
signature are visible. Stubs can only replace visible methods.
•
Interfaces and abstract methods. Stubs provide implementations of interfaces and abstract
methods that can be used in testing. Shims can’t instrument interfaces and abstract
methods, because they don’t have method bodies.
• http://msdn.microsoft.com/en-us/library/hh549175.aspx
Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 9
Writing Unit Tests with Microsoft Fakes
Resources
• MSDN - http://msdn.microsoft.com/enus/library/hh549175.aspx
• 2 Day Unit Testing Course http://www.intertech.com/Training/Microsoft/
Visual-Studio/Unit-Testing/Unit-Testing-inVisual-Studio-2012#axzz2a6ibYDAM
• ALM Rangers eBook http://vsartesttoolingguide.codeplex.com/rel
eases/view/102290
• http://www.peterprovost.org/blog/2012/04/1
5/visual-studio-11-fakes-part-1/
Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 10

similar documents