Unit Testing with Fakes/Moles Framework

The Fakes Framework (called "Moles" during development) is used in .NET unit testing, to isolate dependencies.  A true unit test targets code in the targeted method only, and no other code.  This means the method must be prevented from calling other methods or external resources, such as other classes, assemblies, databases, communications, etc.

Normally, stubs and dependency injection are used to intercept and return results specified by the unit test, to prevent code external to the targeted method from returning unpredictable results that may skew test results.  Fakes provides dependency isolation through means of instrumenting targeted code with detoured calls to delegates specified by the test, and automatically creating stubs from interfaces.

Fakes framework does not remove the need for appropriate use of stubs, nor an excuse for poor architecture.  However, Fakes are extremely useful when working with legacy code that is not properly built for unit testing.

The Moles Isolation Framework has been productized and released as "Fakes", packaged with Visual Studio 2012 Ultimate.  The syntax and concepts are practically identical, with some minor differences.  I will comb through these pages, and provide Fakes syntax, to supplement the Moles.  Ironically, PEX did not make it into production as a hands-on code explorations tool.  PEX is the product from which Moles was spun.

If you have additional questions, please post them to www.StackOverflow.com.  I keep an eye on questions posted with the Moles tag.

Fakes Famework Shims and Stubs
(Coming soon!)

Moles and PEX
  1. What is Pex and Moles?
    These are previous introductory posts to Pex and Moles:
    1. Unit Testing with Moles and Pex (12 APR 2011)
    2. Introducing Moles and PEX (15 JUN 2011)
  2. CodeRush Templates for Pex and Moles
  3. Moles
    1. Basics
      1. What are "Moles assemblies"?
      2. Why doesn't the .Moles assembly appear in Solution Explorer?
      3. What are .MOLES files?
      4. What is a Call Detour?
      5. How Do I Write a Detour?
      6. How Do I Detour a Stub/Mole Type Constructor?
      7. How To Detour a Static Constructor
      8. How Moles Detours Work?
      9. What are Mole and Stub types?
      10. When Do I Use a Stub or Mole Type?
    2. How To guides
      1. How to I Mole an Assembly?
      2. How do I make  my test use Moles?
      3. How do I make my build and/or test servers use Pex and Moles?
      4. How to Mole a Specific Type Instance
      5. How do I Mole .NET Framework Types?
      6. How To Mole the System Assembly
    3. Errors
      1. Moles Error Says I Haven't Instrumented a Type.  What Does That Mean?
      2. "Moles requires tests to be IN an instrumented process"
      3. "Assembly not available in the currently targeted framework"
    4. Problems
      1. Why Are Moles Assemblies Still Added To My Project?
        Steps to properly remove Moles from a project.
  4. Pex
    (More coming about Pex!)

No comments:

Post a Comment

Please provide details, when posting technical comments. If you find an error in sample code or have found bad information/misinformation in a post, please e-mail me details, so I can make corrections as quickly as possible.