Web application developer

Using Moq in a Visual Studio Test Project

monackfr moq

Written by: willem | Jul 28 2013 5:53 PM

In this article I will describe how to use the MOQ library in a basic test project in Visual studio 2012. Visual Studio provides a plain and simple way to make unit tests, but you cannot simply mock objects. Adding the Moq library makes live easier when you are building unit tests.

This post is a walk through for setting up a class library project with a unit test project to test the class library. The following steps will be made:

  • Create a class library project
  • Create a unit test project
  • Create the code to test
  • install the Moq library
  • Create and run the test

You can download the complete solution here.

Create a Class Library Project

Start visual studio 2012, click "file" and then "new project".


In the new project window select under templates "Windows" and in the template list "Class Library". Enter a name (MoqTutorial), a location where you can find it back and click "OK".


Create a Test Project

In the solution explorer, right click the solution, click "add" and select "new project".


Now under templates select "Test" and in the template list "Unit Test Project". Enter a name (MoqTutorialTests), leave the location for what it is so it is next to the other project and click "OK".


Create the code to test

Now we're going to make a small "email handler class". The actual class that send the email is not tested. For this we need an interface that has a SendEmail method, we assume that the concrete email sending class, that implements that interface, works correctly (or is tested elsewhere) so we only need to know if the SendEmail method is called or not.

Right click the MoqTutorial project in your solution and select "add" and then "component".


Now select on the left side Visual C# items and in the list on the right Interface. Enter the name IEmailHandler.cs and click Add.


Add a method definition to the interface with the name SendEmail and save the file.


Now Right click "class1.cs" in the solution explorer and select "rename".


Rename the file to EmailSender.cs, if you get this message:


Click "yes" it will rename the class in the code as well. If you do not get this message, make sure your rename the class name by hand.

Open the EmailSender.cs file. Create a constructor the expects an object of the type IEmailHandler and save that in a private variable. Also create a method called Send. This is the method we are going to test.


We do not add the call to the SendEmail method of the IEmailHandler object yet, because we want our test to fail first. Officially in Test Driven Development (TDD) you create the test first and than the code, but for this tutorial I'd like to keep things clear.

Install the Moq library

Before we can create any tests we need to install the Moq library. Visual Studio has a very handy package manager for that. Open the package manager by clicking "Tools" in the main menu, then "Library package manager" and then "Package manager console".


A console box opens, now select for default project the test project (MoqTutorialTests).


Type in the console "Install-Package moq" and press enter. This command will download all necessary files for using Moq and adds project references to dll's if needed. This action might take a while since it need to download the complete package.

Create and run the test

Right click the Test project and click "Add reference", select the Class Library and click "OK".



Now open the UnitTest1.cs file and add the next references to the code:

using Moq;
using MoqTutorial;

If you chose an other name for your library project, you should change MoqTutorial into your namespace.

Next add the test code to the test method:


In this code I first make a mocked object of the interface we created before. I use this to send to the class we want to test. The mocking framework then administrates what happened with it so I can check if the method I 'm testing is doing it right. Next I create an object of the class I want to test and pass the mocked object as a parameter. I want to test the Send() method of this class so I call it. In the next line I use the moq library to test if the SendEmail method of the handler is called. I do this by passing a lambda and a parameter to see if the method is called exactly one time.

Now open the test explorer (if it is not open yet) via "Test ->  Windows -> Test explorer" in the main menu.

Here you see under "Not run tests" our TestMethod1. Right click it, and select "Run selected tests".


And the test fails!

Why? Well because we "forgot" to call the SendMail method in our Send method of the EmailSender class.

Now add the code to the SendMail method:


Run the test again and it will pass.

Have fun testing!

Download complete solution.


  • Thank you. This was very helpful. I had looked at the Moq documentation but didn't find any sort of tutorial to get me up and running quickly, but this did the trick.

    Al said
  • Thanks a lot for the explanation. It was very helpful !

    rohit said
  • Does your blog have a contact page? I'm having
    problems locating it but, I'd like to shoot you an e-mail.
    I've got some recommendations for your blog you might be interested in hearing.
    Either way, great website and I look forward to seeing it develop over time.

    Google said
  • Its like you read my mind! You seem to know a lot about this,
    like you wrote the book in it or something. I think that you can do with a few pics to drive the message home a little bit, but instead of that, this
    is wonderful blog. An excellent read. I will definitely be back.

    Brown Time said
  • Simply Wow!!!

    Great tutorial, I have been looking into different tutorials, banging my head why I was not able to understand....

    This simple tutorial taught me a lot...

    Great Job...


    Zubair said
  • Hi admin, i must say you have hi quality articles here. Your website should go viral.
    You need initial traffic only. How to get it? Search for: Mertiso's
    tips go viral

    83Betsy said
  • The fun continues even when your base gets assaulted.

    boom beach diamonds said
  • Thanks in favor of sharing such a good opinion, article is fastidious, thats why i have read it fully

    wife share said
  • Very great post. I just stumbled upon your blog and wanted to mention that I
    have really loved surfing around your blog posts. After all I'll be
    subscribing on your feed and I'm hoping you write again soon!

    https://www.viagrapascherfr.com/viagra-prix-maroc-usa/ said
  • Hey there! I know this is kind of off topic but I was wondering if you knew where I could get a
    captcha plugin for my comment form? I'm using the same blog platform as
    yours and I'm having trouble finding one? Thanks a lot!

    https://www.cialissansordonnancefr24.com/generique-tadalafil-20mg-prix/ said
  • Hey there just wanted to give you a quick heads up and let you know a few of the pictures aren't loading properly.
    I'm not sure why but I think its a linking issue.
    I've tried it in two different web browsers and both show the same outcome.

    Xtrasize und Member XXL said
  • I love what you guys tend to be up too. This type of
    clevger work and coverage! Keep up the very good works guys I've incorporated you guys to oour blogroll.

    matedi kuliah manajemen pemasaran

    mata kuliah akuntansi said
  • Greetings from California! I'm bored at work so I decided to browse your blog on my iphone
    during lunch break. I really like the knowledge you provide here and can't wait to take a look when I get home.
    I'm surprised at how quick your blog loaded on my cell phone ..
    I'm not even using WIFI, just 3G .. Anyways, excellent blog!

    penis förtjockning said
Add a Comment

About Me

I'm a web application developer specializing in asp.net. I have skills in asp.net, C#, html5, javascript, asp.net mvc, design patterns and more.

more about me

Random background module

This module for Orchard CMS lets you upload a list of images. For every page it chooses one randomly and sets it a the background image.

Go to project page on CodePlex
Download module

The Monack Framework

This open source framework makes it easy to build and develiver custom web applications. Unfortunately I can't spent much time on it so it is still work in progress. 

More about MonackFr
Project page at CodePlex


Since I decided to create this website in Orchard and didn't know anything about
it when I started, I also post my experiences with this CMS.