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".

moq-in-visual-studio-2012-001

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".

moq-in-visual-studio-2012-002

Create a Test Project

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

moq-in-visual-studio-2012-003

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".

moq-in-visual-studio-2012-004

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".

moq-in-visual-studio-2012-005

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.

moq-in-visual-studio-2012-006

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

moq-in-visual-studio-2012-007

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

moq-in-visual-studio-2012-008

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

moq-in-visual-studio-2012-009

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.

moq-in-visual-studio-2012-010

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".

moq-in-visual-studio-2012-011

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

moq-in-visual-studio-2012-012

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".

moq-in-visual-studio-2012-013

moq-in-visual-studio-2012-014

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:

moq-in-visual-studio-2012-015

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".

moq-in-visual-studio-2012-016

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:

moq-in-visual-studio-2012-017

Run the test again and it will pass.

Have fun testing!

Download complete solution.

9 Comments

  • 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...

    Regards,
    Zubair

    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
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

Orchard

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.