Faking It Isolating Dependencies with Microsoft Fakes
May 25, 2015
Faking ItIsolating Dependencies with Microsoft Fakes
Agenda
Introducing Fakes The Basics Using Stubs Using Shims Advanced Configuration Final Notes Resources
Introducing Fakes
What is it? Where did it come from?
Introducing Fakes
Stub Types
Inheritance based Used when testing a defined contract
Introducing Fakes
Shim Types
Let me start by saying Shims are evil. But they are evil by design. They let you do things you otherwise couldn’t do, which is very powerful. They let you do things you might not want to do, and might know you shouldn’t do, but because of the real world of software, you have to do.
Peter ProvostProgram Manager LeadVisual Studio ALM Tools
Introducing Fakes
Shim Types
Use a profiler to rewrite existing method bodies
Detour virtually any .NET method Performance issues due to the
profiler
The Basics
No additional installation required Isolation type generation driven
by .fakes file Microsoft.QualityTools.Testing.Fakes
assembly/namespace
The Basics
Delegation
Delegates are used to provide the faked functionality
Custom delegate types including support for out and ref parameters
Static class: Microsoft.QualityTools.Testing.Fakes.FakesDeleg
ates
The Basics
Delegation
Overloaded Delegate Types
Action OutAction OutOutAction RefAction RefRefAction
Func OutFunc OutOutFunc RefFunc RefRefFunc
The Basics
Naming
Isolation types are placed in a .Fakes namespace Stub types are prefixed with “Stub” Shim types are prefixed with “Shim” Method parameter types are appended to the
method name “Get” or “Set” is appended to property names
Setters also include parameter type names (like methods)
Generic type arguments are appended to the method name for generic methods
Constructors have parameter type names appended to “Constructor”
Using Stubs
Methods
Assign a delegate to the field that corresponds to the member being isolated
Using Stubs
Properties
Same process as methods Get and Set can be assigned
individually AttachBackingFieldTo[PropertyName]
convenience method
Using Stubs
Events
The framework generates a public method to invoke events [EventName]Event
Using Stubs
Partial Stubs
Allow calling the base implementation when a delegate is not set for a member
Using Stubs
Behaviors
Control what happens when a delegate has not been provided for a member
Set through the InstanceBehavior property
Using Stubs
Stubs Demo
Using Shims
Shim Structure
Shim properties for static members are static on the shim type
Static AllInstances property for applying delegates to all instances of the shim type
Instance properties for applying delegates to a single instance
Using Shims
Instrumentation
ShimsContext Sets up the shim under the IntelliTrace
profiler ShimsContext.Create()
Using Shims
Differences From Stubs
Events don’t have a method to invoke Properties representing the Add and
Remove operations are provided instead Base type members aren’t directly
accessible Static constructors are “shimmable” Bind method for each interface
implemented by the shimmed type Behaviors are controlled by
ShimBehaviors
Using Shims
Shims Demo
Advanced Configuration
Controlling Type Generation Strong Name Signing
Advanced Configuration
Controlling Type Generation
Generating the isolation types is expensive and will extend build time
Configure through the .fakes file Filtering
Advanced Configuration
Controlling Type Generation
Filtering: AttributesAttribute Type Stubs? Shims?
AbstractClasses Boolean ü
FullName String ü ü
Interfaces Boolean ü
Namespace String ü ü
Obsolete* Boolean ü ü
TypeName String ü ü
* Applies only to Remove elements
Advanced Configuration
Controlling Type Generation
Filtering: Matching Tokens
! forces an exact, case sensitive match
* matches the beginning of the string
Advanced Configuration:
Strong Name Signing
Fakes assemblies will be signed with the same key as the underlying assembly unless otherwise specified
Compilation element’s KeyFile attribute in .fakes file
Final Notes
Beware the Docs! Notable Changes from Moles
Notable Changes from Moles Configured through .fakes file rather than .moles Namespace and assembly are .Fakes rather
than .moles Mole types are now shim types Stub prefix is “Stub” rather than “S” Shim prefix is “Shim” rather than “M” ShimsContext replaces HostTypeAttribute Automatic properties can be isolated! Moles allowed for erasing static constructors and
finalizers. Fakes allows shimming static constructors but has no support for finalizers
Moles could control the bitness of the profiler “classes” filter attribute was removed
Resources
Isolating Unit Test Methods with Microsoft Fakeshttp://bit.ly/KYMkyX
Moles Isolation Framework for .NEThttp://bit.ly/LebqZ2
Going Underground: Microsoft Moleshttp://bit.ly/y66D5C