Object mocking in unit testing

If you haven’t gone through my other posts about unit testing, then I would recommend you to please walk through these posts first: All about unit testing and Example of unit testing.

What is Mock?

If you will search in dictionary about mock, then you will find its meaning as below.

Mock meaning in dictionary

An object under test may have some external dependencies like database, file operation I/O, consuming third party libraries or web resources.  Here, We wanted to test the behavior of real object and for that, we need to replace actual object with simulated object in test case.

In simple words, Mocking helps us in creating object that simulates the behavior of real object. Because its impractical to test external dependencies through unit test cases. So our goal here is to check the behavior of the object. It means that after mocking of object, we can verify that simulated object has called database or not, simulated object has performed file I/O operation or not etc.

Example: We have Person entity and we have some business validation around this entity and if everything goes fine then we need to create entry of this Person in database table. Now, we need to test this scenario where you have external dependency on database. So apart from verifying business validation related to Person object, we will also verify whether repository is also being called or not. So lets start with this.

Person repository
PersonRepository.cs

You can use Entity framework, ADO.Net, dapper or any other ORM to connect with database.

Person service
PersonService.cs

We need to test PersonService class because its contains some business logic. It has also external dependency on IRepository<Person>. We have injected IRepository dependency in PersonService class through constructor.

Lets create MSTest project and add PersonTests class. After project creation, you need to add “Moq” nuget package.

Moq nuget
Moq nuget package

Below is the PersonServiceTests class.

PersonTest class
PersonServiceTests.cs

As we know, we have external dependency on PersonRepository, so we have created Mock object of this repository. In setup method, we have passed PersonRepository’s mock  object in the PersonService’s constructor as dependency injection.

In CreatePerson test case, We have setup the PersonRepository.Create() method in arrange section and in Assert section, we have verified that PersonRepository.Create() method must be called only one time. This is behavior driven testing. This unit test case ensure that in positive scenario, person will always be created.

We have used loose mock behavior. You can use strict mock behavior also on mocking object. I would leave up to you which behavior you need to use. Recommendation is to use loose mocking as tests will be cleaned.

This solution is available on GitHub here.  Feel free to clone it and suggest changes. If you have any question / query, feel free to comment here.

Happy testing!

Leave a Reply

Your email address will not be published. Required fields are marked *