Using Moq in a Visual Studio Test Project
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:
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!