Isolating Code under Test with Microsoft Fakes

This article explains how Microsoft Fakes perform a similar role than mocking in unit testing, helping you to isolate the code you are testing in Visual Studio by replacing other parts of the application with stubs or shims. A stub replaces another class with a small substitute that implements the same interface. A shim modifies the compiled code of your application at run time so that instead of making a specified method call, it runs the shim code that your test provides.

You should normally use stubs for calls within your Visual Studio solution and shims for calls to other referenced assemblies. There are also other aspects and contexts to consider like performance; Static methods, sealed types; private methods; interfaces and abstract methods. It is generally recommended touse stub types to isolate from dependencies within your codebase. You can do this by hiding the components behind interfaces. Shim types can be used to isolate from third-party components that do not provide a testable API. The article provides also some “getting started” tips for both stubs and shims, followed by two deeper “how to” sections for each of these concepts. Finally, a section is devoted to discussing options and issues in Fakes code generation and compilation, and describing the naming conventions for Fakes generated types, members, and parameters.