Archives de la catégorie Moles

Moles a Microsoft Framework allowing stub and Mocking

Go to Microsoft url to download Moles and Pex  – (in a new article I will discuss about Pex)

install Moles and Pex:

Moles is a framework which allow to use:

-Stub type  :  Fake

-Mole type : in order to fake some external ressource like IO or external classes.

1/Create a library class where you will call your external ressource like an IO assembly, WCF service… :

-use an interface that your business layer will use to call the external method

-wrap in a class that implement the interface your external resource

2/Create a console application that use your business library:

In solution explorer click on the desired assembly you want to “Mole”, here MyBizService, referenced in BizConsumer and in the context menu click Add mole Assembly

a moles file appear in the solution explorer :

the content of the moles file:

Rebuild your solution  and a new assembly appears:

Double click in MyBizServiceMoles reference to explore the assembly:

Moles create two type of class for each class discovered in the assembly “ Moled”

-Stub class which name begins with S :  for replacing your component you control

-Mole class which name begins with M : to create detours on external component like IO…

Now we create  a unit test on Execute method by right clicking on this method and select create unit test:

click ok on Create Unit Test to create a new Test Project:

enter a name to the new test project:


In solution explorer, you see the new test project and the reference on Mole assembly ( we can add mole assembly after creating test project)

In AssemblyInfo file add a reference assembly to the type you want to mole, here BizService

add Mole Assembly to MyBizService assembly and rebuild Test project :


Go to AssemblyInfo file in test project to add an assembly attribute:

set the MoledType you want , it allows Moles not to mole whole assembly

which optimize Building Time (here we mole only MyExternalRessource class):


Finally, create a unit test with a mole type of your External resource like below (here we rewrite the implementation of method to verify that our external resource is called ( we set a lambda expression for all instances before calling the method that use our external resource):

Conclusion : the external resource is diverted using Mole

You can have some mistake using Mole with the following error message :

The test adapter (‘Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’) required to execute this test could not be loaded. Check that the test adapter is installed properly. Object reference not set to an instance of an object.

the solution is to reinstall mole framework



, , , , , , , ,

Poster un commentaire

Microsoft System Center 2012

All System Center 2012 products, Service Manager, App-Controller, Orchestrator, Virtual Machine Manager, Configuration Manager, Data Protection Manager, Operation Manager -Boutougha Hassan

Microsoft Windows Server 2012

IT - Windows server 2012 - Hassan Boutougha

Microsoft Hyper-V

IT - Hyper-V - Hassan Boutougha

Microsoft SQL Server 2012

IT - SQL Server 2012 - Hassan Boutougha