Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc. 2013 www.Intertech.com 800-866-9884 Slide 1 Writing Unit Tests with Microsoft Fakes."— Presentation transcript:

1 Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc Slide 1 Writing Unit Tests with Microsoft Fakes A Consulting Division Presentation Month Date, 2006 © All Rights Reserved Tim Star /

2 Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc 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

3 Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc 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

4 Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc 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

5 Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc 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) Dont Repeat Yourself (DRY) You Arent Going to Need It (YAGNI) Great resource: Agile Principles, Patterns, and Practices in C#

6 Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc 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

7 Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc 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.

8 Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc 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.

9 Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc 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 cant instrument interfaces and abstract methods, because they dont have method bodies.

10 Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc Slide 10 Resources MSDN - us/library/hh aspxhttp://msdn.microsoft.com/en- us/library/hh aspx 2 Day Unit Testing Course - Visual-Studio/Unit-Testing/Unit-Testing-in- Visual-Studio-2012#axzz2a6ibYDAM Visual-Studio/Unit-Testing/Unit-Testing-in- Visual-Studio-2012#axzz2a6ibYDAM ALM Rangers eBook - eases/view/ eases/view/ /visual-studio-11-fakes-part-1/http://www.peterprovost.org/blog/2012/04/1 5/visual-studio-11-fakes-part-1/


Download ppt "Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc. 2013 www.Intertech.com 800-866-9884 Slide 1 Writing Unit Tests with Microsoft Fakes."

Similar presentations


Ads by Google