Background

Usually, we write some unit tests ourselves to cover the code we wrote because unit tests can verify that the component we wrote works fine when we ran it independently.

Sometimes, software developers attempt to save time by doing minimal unit testing or not writing a line of test code. This is a myth that it saves time because skipping on unit testing leads to higher defect fixing costs during Integration Testing, System Testing, and even Acceptance Testing after the application is completed.

Proper unit testing done during the development stage saves both, time and money, in the end.

Based on my limited experience, there are some reasons that make it hard to write unit tests.

Untestable Code.

No idea of unit tests.

Companies don't care about unit tests.

One-time project

The first two are the key reasons.

In this article, I will share how I write unit tests step by step with a sample.

Let's take a look at the components we will use at first.

Components

There are many components that we can use in .NET Core. In this article, I will demonstrate with components that I use often.

xUnit.net

xUnit.net is a free, open source, community-focused unit testing tool for the .NET Framework. Written by the original inventor of NUnit v2, xUnit.net is the latest technology for unit testing C#, F#, VB.NET, and other .NET languages. xUnit.net works with ReSharper, CodeRush, TestDriven.NET, and Xamarin. It is part of the .NET Foundation and operates under its code of conduct. It is licensed under Apache 2 (an OSI approved license).

Due to the fact that UserInfoBiz depends on IUserInfoRepository and INotifyBiz, both of them are not implemented, and we should not be affected by them when we test UserInfoBiz. So, here, we will use FakeItEasy to mock them.

The following code demonstrates how to mock dependent interface and create the test object of UserInfoBiz.

Now, when we call the CreateUserAsync method of UserInfoBiz, it will always create a new user successfully, no matter if the database is down or there's another infrastructure accident.

Turning to CreateUser_Should_Trigger_SendEmail_When_Add_Succeed--, this test case means that when creating a user successfully, our code should call the SendEmail method of INotifyBiz.

It also means that our code just ensures to call this method, but in UserInfoBiz, we don't care to send email to a user unsuccessfully or successfully, because the result of sending email should consider by INotifyBiz.

Summary

This article showed you an easy sample to demonstrate how to write unit tests for our application via xUnit, FakeItEasy, and Shouldly. Also, it suggested that we all should write some unit tests for our applications. Because it has the following advantages.

Reduces Defects in the Newly developed features or reduces bugs when changing the existing functionality.

Reduces Cost of Testing as defects are captured in a very early phase.

Improves the design and allows better refactoring of code.

Unit Tests, when integrated with build gives the quality of the build as well......