better unit testing with microsoft fakes (rtm)

Download Better unit testing with microsoft fakes (rtm)

Post on 27-Jan-2015

114 views

Category:

Education

2 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

  • 1. VisualStudioALMRangersBetter Unit Testing withMicrosoft Fakes

2. Better Unit Testing with Microsoft Fakes ForewordPage 2 of 96The information contained in this document represents the current view of Microsoft Corporation on the issues discussed asof the date of publication. Because Microsoft must respond to changing market conditions, it should not be interpreted to bea commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after thedate of publication.This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY,AS TO THE INFORMATION IN THIS DOCUMENT.Microsoft grants you a license to this document under the terms of the Creative Commons Attribution 3.0 License. All otherrights are reserved. 2013 Microsoft Corporation.Microsoft, Active Directory, Excel, Internet Explorer, SQL Server, Visual Studio, and Windows are trademarks of the Microsoftgroup of companies.All other trademarks are property of their respective owners. 3. Better Unit Testing with Microsoft Fakes ForewordPage 3 of 96Table of ContentsForeword ............................................................................................................................................................................................................................................5Introduction ......................................................................................................................................................................................................................................7Chapter 1: A Brief Theory of Unit Testing.............................................................................................................................................................................9Software testing.............................................................................................................................................................................................9The fine line between good and flawed unit testing ................................................................................................................... 11Chapter 2: Introducing Microsoft Fakes............................................................................................................................................................................. 15Stubs................................................................................................................................................................................................................ 16Shims............................................................................................................................................................................................................... 18Choosing between a stub and a shim................................................................................................................................................ 19Chapter 3: Migrating to Microsoft Fakes........................................................................................................................................................................... 21Migrating from Moles to Microsoft Fakes........................................................................................................................................ 21Migrating from commercial and open source frameworks....................................................................................................... 23Chapter 4: Miscellaneous Topics........................................................................................................................................................................................... 39Targeting Microsoft .NET Framework 4............................................................................................................................................. 39Adopting Microsoft Fakes in a team................................................................................................................................................... 39You cant Fake everything!...................................................................................................................................................................... 40Verbose logging ......................................................................................................................................................................................... 41Working with strong named assemblies........................................................................................................................................... 41Optimizing the generation of Fakes ................................................................................................................................................... 42Looking under the covers ....................................................................................................................................................................... 43Refactoring code under test .................................................................................................................................................................. 44Removing Fakes from a project............................................................................................................................................................ 44Using Fakes with Team Foundation Version Control................................................................................................................... 45Using Microsoft Fakes with ASP.NET MVC....................................................................................................................................... 47Chapter 5: Advanced Techniques ......................................................................................................................................................................................... 51Dealing with Windows Communication Foundation (WCF) service boundaries .............................................................. 51Dealing with non-deterministic calculations................................................................................................................................... 53Gathering use-case and other analytical information ................................................................................................................. 54Analyzing internal state ........................................................................................................................................................................... 55Avoiding duplication of testing structures....................................................................................................................................... 56Chapter 6: Hands-on Lab.......................................................................................................................................................................................................... 57Exercise 1: Using Stubs to isolate database access (20 - 30 min) ........................................................................................... 57Exercise 2: Using Shims to isolate from file system and date (20 - 30 min) ....................................................................... 67Exercise 3: Using Microsoft Fakes with SharePoint (20 30 min)........................................................................................... 74 4. Better Unit Testing with Microsoft Fakes ForewordPage 4 of 96Exercise 4: Bringing a complex codebase under test (20 30 min)....................................................................................... 79In Conclusion................................................................................................................................................................................................................................. 89Appendix ......................................................................................................................................................................................................................................... 91 5. Better Unit Testing with Microsoft Fakes ForewordPage 5 of 96ForewordFor modern development teams, the value of effective and efficient unit testing is something everyone can agreeon. Fast, reliable, automated tests that enable developers to verify that their code does what they think it should,add significantly to overall code quality. Creating good, effective unit tests is harder than it seems though. Agood unit test is like a good scientific experiment: it isolates as many variables as possible (these are calledcontrol variables) and then validates or rejects a specific hypothesis about what happens when the one variable(the independent variable) changes.Creating code that allows for this kind of isolation puts strain on the design, idioms, and patterns used bydevelopers. In some cases, the code is designed so that isolating one component from another is easy. However,in most other cases, achieving this isolation is very difficult. Often, its so difficult that, for many developers, it isunachievable.First included in Visual Studio 2012, Microsoft Fakes helps you our developers cross this gap. It makes iteasier and faster to create well-isolated unit tests when you do have systems that are testable, letting you focuson writing good tests and not on test plumbing. It also enables you to isolate and test code that is nottraditionally easy to test, by using a technology called Shims. Shims use runtime interception to let you detouraround challenging dependencies and replace them with something you can control. As we have mentioned,being able to create this control variable is imperative when creating high-quality, f