22 November 2011

How To Detour a Static Constructor

Static constructors pose a challenge.  Naturally, one can not call the constructor of a static class.  This also means that the Moles Framework does not provide an accessor to detour the constructor of a static class.  However, Moles provides a way to erase static constructors, to prevent them from executing.  Simply add the MolesDisableStaticConstructor assembly attribute to the test class:


15 November 2011

Partial Stubs

Partial stubs are used when you want to stub only some of the members of a stub or mole type, and allow the others to fall through to their original code.  This is useful, when only some of the members of a class handle a dependency object, but not others.  After all, the purpose of Moles is to isolate dependencies.  It is always best to leave the smallest footprint possible, when dealing with isolation.


14 November 2011

How Do I Detour a Mole Type Constructor?

Please be aware this information is based on Moles version v0.94.51023.0, 10/23/2010, and may not be accurate for prior versions.

Stub type constructors are not able to be detoured; because, they should be instantiated in the test, and have no need to be detoured.  Subsequently, no constructor detour appears in the stub type.

Constructors are located in the mole type root, and are named "Constructor", followed by the argument type names.  For example, the following are constructor detours for System.IO.Moles.MFile:

09 November 2011

How Do I Write a Detour?

If you don't use lambda expressions, take a couple of hours right now to learn it, and vastly improve your skill set.  I can wait...

Now that we all know lambda expressions are simple delegates set to "super easy" mode, you already know how to write a detour!  Detours are set the same way in both stub and mole types.  You have two options, when detouring a call to a stub or moles type member.

Granted, these code examples are very simplistic, but you can use these to your advantage. See the Tricks With Detours section of the Testing With Microsoft Pex and Moles page, for some ideas.

Why Are Moles Assemblies Still Added To My Project?

Most people using moles, like Dave, encounter the situation where they want to remove Moles from the project.  You delete the hidden MolesAssemblies folder, but Moles just won't go away!  How do you remove moles from a project?

When Do I Use a Stub or Mole Type?

Give preference to Stub types.  Using a stub type helps you adhere to sound architecture principles, when writing new code and/or using Test Driven Development (TDD).

What Are Mole and Stub Types?

Moles generates two types, in the Moles assemblies: Mole types and Stub types.  Both behave very much the same, but are are used in mutually exclusive situations.  See page 3 of the Moles Reference Manual, for more information on stub and mole types.

An important concept often overlooked by developers investigating the Moles Framework is that the mole and Stub types may be configured with frequently-used detours, in a private, parameterized test class method.  This allows you to centralize the setting of frequently-used values, and therefore adhering to the DRY principle.

02 November 2011

How Do Moles Detours Work?

Moles is a dependency isolation and mocking framework.  Instead of building mock stubs for dependency injection, Moles allows the programmer to detour the call to portions of the actual dependency object.  So, how does Moles detour calls, without touching the source code?

What is a Call Detour?

Detouring a call means the call to the target object (method, property, etc.) is redirected to another object.  That other object will be an anonymous method or other method in the test class.  Detours are delegates, generally managed through a Func< , > object.