writing unit tests with microsoft fakes copyright © intertech, inc. 2013 800-866-9884 slide 1...

10
Writing Unit Tests with Microsoft Fakes ght © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 1 Writing Unit Tests with Microsoft Fakes A Consulting Division Presentation Month Date, 2006 — © — All Rights Reserved Tim Star [email protected] http://www.intertech.com/Blog/ http://timsbrownbaglunch.blogspot.com/ /

Upload: sade-beaver

Post on 30-Mar-2015

220 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc. 2013  800-866-9884 Slide 1 Writing Unit Tests with Microsoft Fakes

Writing Unit Tests with Microsoft Fakes

Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 1

Writing Unit Tests with Microsoft Fakes

A Consulting Division Presentation

Month Date, 2006 — © — All Rights Reserved

Tim Star

[email protected]://www.intertech.com/Blog/http://timsbrownbaglunch.blogspot.com/ /

Page 2: Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc. 2013  800-866-9884 Slide 1 Writing Unit Tests with Microsoft Fakes

Writing Unit Tests with Microsoft Fakes

Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 2

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

Page 3: Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc. 2013  800-866-9884 Slide 1 Writing Unit Tests with Microsoft Fakes

Writing Unit Tests with Microsoft Fakes

Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 3

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

Page 4: Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc. 2013  800-866-9884 Slide 1 Writing Unit Tests with Microsoft Fakes

Writing Unit Tests with Microsoft Fakes

Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 4

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

Page 5: Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc. 2013  800-866-9884 Slide 1 Writing Unit Tests with Microsoft Fakes

Writing Unit Tests with Microsoft Fakes

Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 5

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#”

Page 6: Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc. 2013  800-866-9884 Slide 1 Writing Unit Tests with Microsoft Fakes

Writing Unit Tests with Microsoft Fakes

Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 6

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

Page 7: Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc. 2013  800-866-9884 Slide 1 Writing Unit Tests with Microsoft Fakes

Writing Unit Tests with Microsoft Fakes

Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 7

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.

Page 8: Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc. 2013  800-866-9884 Slide 1 Writing Unit Tests with Microsoft Fakes

Writing Unit Tests with Microsoft Fakes

Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 8

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.

Page 9: Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc. 2013  800-866-9884 Slide 1 Writing Unit Tests with Microsoft Fakes

Writing Unit Tests with Microsoft Fakes

Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 9

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

Page 10: Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc. 2013  800-866-9884 Slide 1 Writing Unit Tests with Microsoft Fakes

Writing Unit Tests with Microsoft Fakes

Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 10

Resources

• MSDN - http://msdn.microsoft.com/en-us/library/hh549175.aspx

• 2 Day Unit Testing Course - http://www.intertech.com/Training/Microsoft/Visual-Studio/Unit-Testing/Unit-Testing-in-Visual-Studio-2012#axzz2a6ibYDAM

• ALM Rangers eBook - http://vsartesttoolingguide.codeplex.com/releases/view/102290

• http://www.peterprovost.org/blog/2012/04/15/visual-studio-11-fakes-part-1/