Top Banner
Faking It Isolating Dependencies with Microsoft Fakes
28

Faking it

May 25, 2015

Download

Technology

Dave Fancher

Proper unit testing involves identifying the smallest pieces of testable code and ensuring that they function properly. Testing these units effectively generally requires somehow isolating them from the rest of the code base. There are a number of frameworks that help with this isolation but the Fakes isolation framework goes beyond interfaces and virtual methods by allowing us to detour any framework method. In this session we’ll explore how stubs and shims can help us isolate testable units regardless of whether we control the dependencies.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Faking it

Faking ItIsolating Dependencies with Microsoft Fakes

Page 2: Faking it

Agenda

Introducing Fakes The Basics Using Stubs Using Shims Advanced Configuration Final Notes Resources

Page 3: Faking it

Introducing Fakes

What is it? Where did it come from?

Page 4: Faking it

Introducing Fakes

Stub Types

Inheritance based Used when testing a defined contract

Page 5: Faking it

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

Page 6: Faking it

Introducing Fakes

Shim Types

Use a profiler to rewrite existing method bodies

Detour virtually any .NET method Performance issues due to the

profiler

Page 7: Faking it

The Basics

No additional installation required Isolation type generation driven

by .fakes file Microsoft.QualityTools.Testing.Fakes

assembly/namespace

Page 8: Faking it

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

Page 9: Faking it

The Basics

Delegation

Overloaded Delegate Types

Action OutAction OutOutAction RefAction RefRefAction

Func OutFunc OutOutFunc RefFunc RefRefFunc

Page 10: Faking it

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”

Page 11: Faking it

Using Stubs

Methods

Assign a delegate to the field that corresponds to the member being isolated

Page 12: Faking it

Using Stubs

Properties

Same process as methods Get and Set can be assigned

individually AttachBackingFieldTo[PropertyName]

convenience method

Page 13: Faking it

Using Stubs

Events

The framework generates a public method to invoke events [EventName]Event

Page 14: Faking it

Using Stubs

Partial Stubs

Allow calling the base implementation when a delegate is not set for a member

Page 15: Faking it

Using Stubs

Behaviors

Control what happens when a delegate has not been provided for a member

Set through the InstanceBehavior property

Page 16: Faking it

Using Stubs

Stubs Demo

Page 17: Faking it

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

Page 18: Faking it

Using Shims

Instrumentation

ShimsContext Sets up the shim under the IntelliTrace

profiler ShimsContext.Create()

Page 19: Faking it

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

Page 20: Faking it

Using Shims

Shims Demo

Page 21: Faking it

Advanced Configuration

Controlling Type Generation Strong Name Signing

Page 22: Faking it

Advanced Configuration

Controlling Type Generation

Generating the isolation types is expensive and will extend build time

Configure through the .fakes file Filtering

Page 23: Faking it

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

Page 24: Faking it

Advanced Configuration

Controlling Type Generation

Filtering: Matching Tokens

! forces an exact, case sensitive match

* matches the beginning of the string

Page 25: Faking it

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

Page 26: Faking it

Final Notes

Beware the Docs! Notable Changes from Moles

Page 27: Faking it

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

Page 28: Faking it

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