Showing posts with label moles. Show all posts
Showing posts with label moles. Show all posts

20 September 2012

Fakes added to Moles posts

Today, I am (finally) starting to update all of my posts on use of the Moles Isolation Framework to its official release version, called "Fakes Framework", included in Visual Studio 2012 Ultimate and newer.

To learn how to use both Moles and Fakes, click the "Unit Testing with Fakes/Moles Framework" link, below the page title on this blog.  Please bear with me, while I get the information updated.  I am very glad to see Moles come to market!

15 December 2011

Pex and Moles to be Visual Studio 11 Feature Set

In true, "I will neither confirm nor deny" Microsoft fashion, a little bird in Redmond informed me that Pex and Moles will have no further releases, because they are being "productized". Another, larger bird stated, "dependency isolation will be taken care of, in Visual Studio 2012," when I asked if Moles and Pex will be rolled into VS 2012.

The Visual Studio 2012 community technical preview (CTP) is available for download to anybody.  However, the Moles and Pex products are not yet integrated into the application.  There are several points of integration the need to be perfected, before adding it.

Note, this VS11 download is a BETA version, and is incomplete.  Many more features are still cooking.  Stability is OK, but use it at your own risk!  Visual Studio 2012 Beta will (should) receive updates from Microsoft Update, to keep it current with the newest release.


(This post is extracted from the Testing with Microsoft Pex and Moles page of this blog.  I felt this information was worth putting in its own post, after sitting in a sidebar, since May 2011.)

UPDATE: Changed the link and name to reflect the Beta release.

How To Mole the System Assembly

The System namespace may be moled, in addition to any other child namespace.  For instance, you may want to detour calls away from System.StringComparer, which is a member of the System assembly.   To mole members of the .NET Framework "System" namespace, follow these four steps:

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:

[assemblyMolesEraseStaticConstructor(typeof(MyStatic))]

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.

(See

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.

24 October 2011

What are .MOLES Files?

After clicking the Add Moles Assembly option, in the Solution Explorer context menu, a file with extension .moles is added to the test project.  This file contains an instruction for the compiler, that requests a mole assembly to be generated for the specified assembly.  Without these files, no moles assemblies will be generated.

Moles Error Says I Haven't Instrumented a Type. What Does That Mean?

There are three possible causes for instrumentation exceptions:

Using Declarations and Assembly Attributes
You must ensure a MoledType assembly attribute is present, for the type specified by the exception.  For example:

[assemblyMoledType(typeof(System.IO.File))]

Refer to the How Do I Make My Test Use Moles post, for more details.


Mode of Execution
If you are certain you have included all of the required using declarations and assembly attributes, examine your method of execution.  Pex and Moles tests require special instructions to be sent to the compiler, to properly route calls through the Moles assembly.  Third party test execution utilities, such as CodeRush, do not provide this supplemental information.  A missing instrumentation exception will be thrown, when Pex and Moles tests are executed by CodeRush.


Unsupported Test Framework
Pex and Moles support both the Visual Studio and NUnit Test Frameworks.  Other frameworks are not supported.

Why Doesn't the .Moles Assembly Appear in Solution Explorer?

There are two common answers to this question:
  1. Compile the test project.  The moles assemblies are generated on compile.
  2. Select the project in the Solution Explorer window, and then click the Show All Files button.  A hidden folder named MolesAssemblies will appear.
There should be no reason to handle the moles assemblies, since they may be frequently regenerated.

Where Are the Moles Assemblies?

"I compiled my test project, after asking to mole an assembly.  I see the .moles file, but where is the moles assembly?"

Moles assemblies are visible in the Solution Explorer window, by clicking the Show All Files button.  (Be sure the proper project is selected, first!) All moles assemblies are found in the hidden MolesAssemblies folder.

How Do I Make My Build and/or Test Servers Use Pex and Moles?

All build and test servers (e.g. MSBuild) that must handle Pex and/or Moles have the Pex and Moles Framework installed.  After installation, the builds and tests should execute normally.

22 October 2011

How do I make my test use Moles?

NOTE: The Moles isolation framework must be installed to any machine that will compile the solution and/or execute the mole tests.

The following things must be done, to use Moles in a test.  This looks like a lot, now.  However, you'll quickly get the hang of things, and have these steps done in seconds!  Using my CodeRush Moles templates will help speed things up.

21 October 2011

How Do I Mole .NET Framework Types?

Moles ships with pre-compiled moles assemblies for the entire .NET Framework (mscorlib).  (This used to be a huge pain to compile!)  Adding the moled mscorlib is easy.  Just follow these three steps:

11 October 2011

How do I Mole an Assembly?

Creating a Moles wrapper assembly ("moled" assembly) has been greatly simplified:
  1. Expand the test project node, in the Solution Explorer window
  2. Expand the References child node
  3. Right-click the assembly you wish to mole -- the context menu appears
  4. In the context menu, select the Add Moles Assembly option -- a .moles file appears in the text project
  5. Build the test project