Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Faking it

2,790 views

Published on

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.

Published in: Technology
  • Be the first to comment

Faking it

  1. 1. Isolating Dependencies with Microsoft Fakes
  2. 2.  Introducing Fakes The Basics Using Stubs Using Shims Advanced Configuration Final Notes Resources
  3. 3.  What is it? Where did it come from?
  4. 4.  Inheritance based Used when testing a defined contract
  5. 5. Let me start by saying Shims are evil. But they areevil by design. They let you do things you otherwisecouldn’t do, which is very powerful. They let you dothings you might not want to do, and might knowyou shouldn’t do, but because of the real world ofsoftware, you have to do. Peter Provost Program Manager Lead Visual Studio ALM Tools
  6. 6.  Use a profiler to rewrite existing method bodies Detour virtually any .NET method Performance issues due to the profiler
  7. 7.  No additional installation required Isolation type generation driven by .fakes file Microsoft.QualityTools.Testing.Fakes assembly/namespace
  8. 8.  Delegates are used to provide the faked functionality Custom delegate types including support for out and ref parameters Static class:  Microsoft.QualityTools.Testing.Fakes.FakesDelegates
  9. 9.  Action  Func OutAction  OutFunc OutOutAction  OutOutFunc RefAction  RefFunc RefRefAction  RefRefFunc
  10. 10.  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”
  11. 11.  Assign a delegate to the field that corresponds to the member being isolated
  12. 12.  Same process as methods Get and Set can be assigned individually AttachBackingFieldTo[PropertyName] convenience method
  13. 13.  The framework generates a public method to invoke events  [EventName]Event
  14. 14.  Allow calling the base implementation when a delegate is not set for a member
  15. 15.  Control what happens when a delegate has not been provided for a member Set through the InstanceBehavior property
  16. 16.  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
  17. 17.  ShimsContext  Sets up the shim under the IntelliTrace profiler  ShimsContext.Create()
  18. 18.  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
  19. 19.  Controlling Type Generation Strong Name Signing
  20. 20.  Generating the isolation types is expensive and will extend build time Configure through the .fakes file Filtering
  21. 21. Attribute Type Stubs? Shims?AbstractClasses Boolean FullName String  Interfaces Boolean Namespace String  Obsolete* Boolean  TypeName String   * Applies only to Remove elements
  22. 22.  ! forces an exact, case sensitive match * matches the beginning of the string
  23. 23.  Fakes assemblies will be signed with the same key as the underlying assembly unless otherwise specified Compilation element’s KeyFile attribute in .fakes file
  24. 24.  Beware the Docs! Notable Changes from Moles
  25. 25.  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
  26. 26.  Isolating Unit Test Methods with Microsoft Fakes http://bit.ly/KYMkyX Moles Isolation Framework for .NET http://bit.ly/LebqZ2 Going Underground: Microsoft Moles http://bit.ly/y66D5C

×