NUnit & Moq

Author: Mir Md. Faysal
Senior Software Engineer
Full Stack Engineer / AI & Cognitive Systems / JavaScript

Unit testing is a process through which the functionality of a small unit of software (i.e. a method) is tested. That small unit may have other external dependencies.

However, during Unit Testing, we must focus on testing the logic of only that unit and hence must control the impact of the dependencies.

To ensure this, we use a technique called “mocking”.

Mocking is a process where dependencies are replaced by controlled replacements that simulate the behavior of the real dependencies.

NUnit is a unit testing framework for .NET languages, and Moq is the most popular mocking framework for .NET.

I assume the reader is familiar with C# projects, Visual Studio IDE, and the Nuget package manager for managing dependencies.

I will cover creating Unit tests using NUnit and Moq.

We will create a sample .NET console project using Microsoft Visual Studio 2015 IDE. This sample project will calculate an employee’s tax amount. This will be a simple application. I will describe the step-by-step process of creating the test project, adding unit tests using NUnit, and mocking the dependencies using moq.

Console Project hands-on

Let’s create a console project called EmployeeApp.

We will create some Model, Repository and Service classes.

The model class Employee with some basic properties will look like this:

Let’s create the Employee repository class that will hold the sample Employee list, and will have a function that returns an employee by the employee’s Id.

At this point, we will create the Employee Service that will contain the logic for calculating the employee’s tax. The logic is simple – calculate the total number of months of employment, multiply that by the employee’s monthly salary, and then multiply the tax rate.

For obtaining the tax rate, let’s create another service called TaxService that will be used by the Employee Service.

At this stage, we have created a console application that can calculate an employee’s tax. The project structure will look something like this:

Now we will add the test project to our solution.

We will create the EmployeeServiceUnitTest.cs file in the test project as we will write unit tests for the Employee Service. We will install NUnit and Moq using the Nuget package manager.

Make sure that in your references, NUnit and Moq are present after installation:

For running NUnit tests and exploring the tests, we need to install a visual studio extension called “NUnit 3 Test Adapter”.

As we are going to test the Employee Service of the Employee App, we need to add the reference of the EmployeeApp in our Test.EmployeeApp project.

We are going to use the following attributes:

  • TestFixture
  • SetUp
  • OneTimeSetUp
  • Test

TextFixture is the attribute used to mark a class that contains unit tests.

SetUp is used to mark the method that will be called every time a test is run.

OneTimeSetUp marks a method to be called a single time after the test class is initialized.

Test is the attribute that marks any method as a test case.

As we should know, for unit testing a class, all other dependencies need to be mocked to control their behaviour. EmployeeService depends on a repository and another service called TaxService. Therefore, we need to mock the behaviour of the repository and the Tax Service. The initial unit test class will then look like this.

MockBehavior.Strict restricts any method of those mocked classes (Repository and TaxService) to have a mock setup.

Unit test methods can have three parts:

  • Arrange
  • Act
  • Assert

In Arrange we setup the test i.e. initialize mock objects, create the object of the target class, initialize other variables, etc.

In Act we invoke the method to test.

In Assert we check and verify the returned results with the expected result.

In our test method, we will check and verify the CalculateTax method of Employee Service. As the CalculateTax method fetches the employee from the repository and gets the tax rate from Tax Service, we will have to mock the Get method of the repository and the GetTaxRate method of Tax Service.

Then in Assert we will check if the returned value is calculated as per our business logic.

We can explore the test cases and pass/fail status in the Unit test session window:

We have successfully written a unit test using NUnit and Moq for mocking dependencies.

Stay tuned for further advanced topics of unit testing using NUnit & mocking dependencies with Moq.