better unit testing with microsoftآ® fakes s1. better unit testing with microsoft fakes –...

Download Better Unit Testing with Microsoftآ® Fakes s1. Better Unit Testing with Microsoft Fakes – Introduction

If you can't read please download the document

Post on 22-May-2020

1 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • V is

    u a l S tu

    d io

    A LM

    R a n g

    e rs

    Better Unit Testing with

    Microsoft® Fakes

    v1.2

  • Better Unit Testing with Microsoft Fakes – Foreword

    Page 2 of 79

    The information contained in this document represents the current view of Microsoft Corporation on the issues discussed as

    of the date of publication. Because Microsoft must respond to changing market conditions, it should not be interpreted to be

    a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the

    date 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 other

    rights are reserved.

     2013 – 2014 Microsoft Corporation.

    Microsoft, Active Directory, Excel, Internet Explorer, SQL Server, Visual Studio, and Windows are trademarks of the Microsoft

    group of companies.

    All other trademarks are property of their respective owners.

  • Better Unit Testing with Microsoft Fakes – Foreword

    Page 3 of 79

    Table of Contents Foreword ............................................................................................................................................................................................................................................ 4

    Introduction ...................................................................................................................................................................................................................................... 5

    Chapter 1: A Brief Theory of Unit Testing ............................................................................................................................................................................. 7

    Software testing ............................................................................................................................................................................................. 7

    The fine line between good and flawed unit testing ...................................................................................................................... 9

    Chapter 2: Introducing Microsoft Fakes ............................................................................................................................................................................. 12

    Stubs ................................................................................................................................................................................................................ 13

    Shims ............................................................................................................................................................................................................... 15

    Choosing between a stub and a shim ................................................................................................................................................ 16

    Chapter 3: Migrating to Microsoft Fakes ........................................................................................................................................................................... 17

    Migrating from Moles to Microsoft Fakes ........................................................................................................................................ 17

    Migrating from commercial and open source frameworks ....................................................................................................... 19

    Chapter 4: Miscellaneous Topics ........................................................................................................................................................................................... 33

    Targeting Microsoft .NET Framework 4 ............................................................................................................................................. 33

    Adopting Microsoft Fakes in a team................................................................................................................................................... 33

    You can’t Fake everything!...................................................................................................................................................................... 33

    Verbose logging ......................................................................................................................................................................................... 34

    Working with strong named assemblies ........................................................................................................................................... 35

    Optimizing the generation of Fakes ................................................................................................................................................... 35

    Looking under the covers ....................................................................................................................................................................... 36

    Working with Code Contracts ............................................................................................................................................................... 37

    Refactoring code under test .................................................................................................................................................................. 37

    Removing Fakes from a project ............................................................................................................................................................ 38

    Using Fakes with Team Foundation Version Control ................................................................................................................... 39

    Using Microsoft Fakes with ASP.NET MVC ....................................................................................................................................... 41

    Chapter 5: Advanced Techniques ......................................................................................................................................................................................... 44

    Dealing with Windows Communication Foundation (WCF) service boundaries .............................................................. 44

    Dealing with non-deterministic calculations ................................................................................................................................... 46

    Gathering use-case and other analytical information ................................................................................................................. 47

    Analyzing internal state ........................................................................................................................................................................... 48

    Avoiding duplication of testing structures ....................................................................................................................................... 49

    Chapter 6: Hands-on Lab.......................................................................................................................................................................................................... 50

    In Conclusion ................................................................................................................................................................................................................................. 77

  • Better Unit Testing with Microsoft Fakes – Foreword

    Page 4 of 79

    Foreword For modern development teams, the value of effective and efficient unit testing is something everyone can agree

    on. 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. A

    good unit test is like a good scientific experiment: it isolates as many variables as possible (these are called

    control 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 by

    developers. 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, it’s so difficult that, for many developers, it is

    unachievable.

    First included in Visual Studio 2012, Microsoft Fakes helps you — our developers — cross this gap. It makes it

    easier and faster to create well-isolated unit tests when you do have systems that are “testable,” letting you focus

    on writing good tests and not on test plumbing. It also enables you to isolate and test code that is not

    traditionally easy to test, by using a technology called Shims. Shims use runtime interception to let you detour

    around challengi