Unit testing made easy Part 1: Repository Testing

Introduction

We are all familiar with a very common scenario while coding in a business application. In fact, this is the most frequent scenario where we want to save an object to the database. But how would we know that our code is functioning properly? Well there are many ways we could know. Some are good ways to follow and some are not. Let’s discuss using a scenario.

Example Scenario

Suppose we want to save a Student object where the class will have Name, Phone, Email and Id. To save this object we should create a repository class named StudentRepository. In this class we will write our data saving codes.

Now there are some points I want to mention. It is good practice to create an interface for each of the repository class. It is a subset of Repository Pattern.

In our case, we should create IStudentRepository where we will define only Save method and make the StudentRepository class to implement this interface.

So, in summary we have three projects. Those are DataAccess, DataAccess.Contract, and Domain up to this state. Below are the class definitions and project architecture.

Entity Framework

For implementing the database access code, we will not use traditional SqlClient library classes and don't write the traditional sql queries. Rather we will use the Entity Framework instead. Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write.

To use that framework in our project we need to install that. But where would we find that library? Well. Microsoft has developed an extension by which we can install, update and uninstall the external libraries to our project in a very easy way. Below is how can we install the Entity Framework into our DataAccess Project..

Walkthrough: Install Entity Framework using Nuget

Right click on the reference option of the DataAccess project.

Find the Entity Framework item (second in the below picture).

Click the “install” button of that item.

Click Accept when you are asked for the license agreement.

After successful installation, you should see the green sign instead of the “install” button.

After installing the entity framework, we should create a class DataContext which will inherit DBContext class and we will map our class Student with the table name
under OnModelCreating method. When the DataContext class is called for the first time, the Entity Framework will look for the Database and create the database
by itself if that is not exists. Next, it will create the tables according to the instruction we gave in OnModelCreating method. From the below class,
the system will create a database named MockingPractices.DataAccess.DataContext in .\SQLEXPRESS server (since we didn't give any connection string it will
do it in the default server). And also the Student table is created.

Unit Testing

Now we will find a way to test our codes. In a tradition way, we would have to create a Simple User Interface (might be Windows Forms Application) where there will be a button. If we click the button, we will create a student object and pass it to the repository method. Below is the scenario.

Class diagram

User Interface based testing architecture

But if we frequently change our code and need to test, then every time we must run the UI and hit the button for test. Things got worse if we need to test multiple scenarios for a single repository save method. So, what we can understand, we need a system which can manage automatically the calling of those methods. We don’t need to click and click the user interface for each change of the development code. Below may be one of the proposed architecture.

Automated testing architecture

This type of testing is named as automated unit testing and currently there is a couple of frameworks for this purpose. NUnit and Visual Studio’s internal Unit testing framework both are very common. Let’s use the second one in our solution. Below is how can you create a Unit Test project to your solution. If you still don't get the idea, don't worry. Just keep reading below. " />

Go to: Add New Project-->Visual C# --> Test --> Unit Test Project

Give appropriate name and then press OK button

Each of the unit testing frameworks has some classes by which the compiler understand to which classes should it run. This tag is TestClass for this and the required test methods must have TestMethod tag on top of it.

In our case, we can rename the default class to StudentRepositoryTest class and rename the given template method to SaveShouldReturnSavedId method.

So, the test method now looks like below.

Philosophy of the Unit Testing

Let’s begin with a very sad but true scenario. Suppose one day you are in romantic mode, want to talk with your lover and you say “I Love You Jaan”. What do you expect to hear? We all expect “I love you too honey” or similar types of line in reply. But if your lover shouts at you then that shouting will definitely not be matched with your expectation. Am I right?

In programming world, we expect our method to work smoothly without any problem. We exactly know what to expect from a specific method. In our scenario, we expect our repository method to save the student object and returns us the saved id of that object. And we also know that Id must not be zero because our database’s primary key is auto incremented number and there is no way for the object to be saved in the id 0 row. So, our expectation is the returned value should be not equal to zero. Right? Make sense?

So how would we implement that scenario in our code?

There is a class named Assert made by Microsoft. Its duty is to match the expected value to the returned/actual value. If the expectation matches with the actual, it shows green but if the expectation mismatched, it shows red. So the code will looks like below.

Run as many as you like the test just pressing the Run command. You will never need to open the user interface again and again and input the values and calculate
the expected values by yourself. This is the true beauty of unit testing.

Share

About the Author

I am Software Engineer currently working in Secure Link Services Ltd. (http://selise.ch) and I also take classes on various topics of .NET Technologies at BASIS .NET Training Program (http://www.basis.org.bd/index.php/training), which is the best training program in Bangladesh.

Comments and Discussions

This shows the use of MSTest, not NUnit as the article describes.
Using a real database is completely missing the point of UNIT testing. I'd call it integration testing, it has value, but it isn't unit testing and shouldn't be the starting point of testing.
It also misses the point about using interfaces to the repository.
It is a well-presented article introducing integration tests, however.

Unit testing doesn't belongs to NUnit only. I didn't mention NUnit testing.
May be its called integration testing, but the primary focus of my post is to enable the readers to write test code.
Adding more text about Repository pattern just made the post more big. But i will consider adding those here. Thanks.
I will study about Integration Testing to compare with unit testing. Thanks.

As I said, it's a good article on testing, but just not on unit testing.[^].
The key difference is that unit testing exercises only the code under test. The point of interfaces for entities like the database repository is so that you can mock or stub them during unit testing.

An integration test[^] is free to exercise more of a collaboration of code, such as including the database layer.

There's much to be said on the pros and cons of each - worthy of many CodeProject articles in their own right.