Method delegate using Moles

Posted by in Visual Studio category on for Beginner level | Points: 250 | Views : 14363 red flag
Rating: 3 out of 5  
 1 vote(s)

Moles are another Visual Studio add-in along with Pex. We already discussed about Pex, how to create Unit test using Pex and Parameterized Unit tests in another article.

We already discussed about Pex and how to generate Unit Test. In this article we will discuss about Moles and how to use moles for mock or delegate a method and property.

Install Moles from

Create Mole

For using Moles, first we need to create the Moles of an assembly. Add the assembly as a reference to your test project. Right click on the assembly under References and select Add Moles Assembly.

This will add the following files and references to your project.


Now we can mock any classes, methods & properties inside this assembly.

Mock or delegate a Property

In our sample, the SampleClass contains a Dictionary property.

public class SampleClass1


        public Dictionary<stringstring> Data

        {             get;






For passing meaningful data to our parameterized test, we need to mock the dictionary and pass custom dictionary to it. We can mock it using


Dictionary<stringstring> dummy=new Dictionary<string,string>();
            SampleApp.Moles.MSampleClass1.AllInstances.DataGet = ((b) => dummy);

All instances of SampleClass1 will receive the data specified to the Mole class.
As we noticed, after adding the assembly Mole, we will receive a new class with name prefixed with M. All mock operations will be performed using this new Mole class.

Mock A Method

In our SampleClass, we have a method, which returns some value.

public string GetDetails(string data)

        {                 ---------------------------


            return Data.First().Value;


For mocking the same, 

SampleApp.Moles.MSampleClass1.AllInstances.GetDetailsString =((a,b) => "Dummy Data");

Here, ‘a’ refers the class instance and ‘b’ is the argument.


Pex and Moles are Visual Studio Add-ins for supporting the Unit test Framework. We will explore more on Pex and Moles in next article.


Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Ambily KK
Member Level: Silver
Member Status: Member,Microsoft_MVP,MVP
Member Since: 5/18/2010 1:05:25 AM
Country: India
Thanks Ambily K K
I have over 9 years of experience working on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP .Net and Ajax. My interests also include Office Open XML, Azure, Visual Studio 2010. Technology adoption and learning is my key strength and technology sharing is my passion.

Login to vote for this post.

Comments or Responses

Posted by: Bhavanib on: 10/8/2012 | Points: 25
Hi Ambily,

Very nice to see your article.I have some problem while mocking third party dlls.
Actually in my application we are using thrid party project and we customized for my requirement.In this project there is a base class

public partial class CatalogBusiness : ZNodeBusinessBase

The ZNodeBusinessBase class is the base class(Znode.Framework.Business).Just we want to mock that class.How we can mock?
Just we added ZNode.Framework.Business add to Moles assembly.After that i dont know how i am going to do all the operation through coding side.
Please can you suggest me.

Waiting for your reply


Login to post response

Comment using Facebook(Author doesn't get notification)