Book Image

C# and .NET Core Test Driven Development

By : Ayobami Adewole
Book Image

C# and .NET Core Test Driven Development

By: Ayobami Adewole

Overview of this book

This book guides developers to create robust, production-ready C# 7 and .NET Core applications through the practice of test-driven development process. In C# and .NET Core Test-Driven Development, you will learn the different stages of the TDD life cycle, basics of TDD, best practices, and anti-patterns. It will teach you how to create an ASP.NET Core MVC sample application, write testable code with SOLID principles and set up a dependency injection for your sample application. Next, you will learn the xUnit testing framework and learn how to use its attributes and assertions. You’ll see how to create data-driven unit tests and mock dependencies in your code. You will understand the difference between running and debugging your tests on .NET Core on LINUX versus Windows and Visual Studio. As you move forward, you will be able to create a healthy continuous integration process for your sample application using GitHub, TeamCity, Cake, and Microsoft VSTS. By the end of this book, you will have learned how to write clean and robust code through the effective practice of TDD, set up CI build steps to test and build applications as well as how to package application for deployment on NuGet.
Table of Contents (11 chapters)

The TDD cycle

The TDD technique follows a tenet known as the red-green-refactor cycle, with the red state being the initial state, indicating the commencement of a TDD cycle. At the red state, the test has just been written and will fail when it is run.

The next state is the green state and it shows that the test has passed after the actual application code has been written. Code refactoring is essential to ensure code completeness and robustness. Refactoring will be repeatedly done until the code meets performance and requirement expectations:

At the beginning of the cycle, the production code to run the test against has not been written, so it is expected that the test will fail. For example, in the following code snippet, the IsServerOnline method has not been implemented yet, and when the Test_IsServerOnline_ShouldReturnTrue unit test method is run, it should fail:

public bool IsServerOnline()
{
return false;
}

[Fact]
public void Test_IsServerOnline_ShouldReturnTrue()
{
bool isOnline=IsServerOnline();

Assert.True(isOnline);
}

For the test to pass, you have to implement the production code iteratively. When the following IsServerOnline method is implemented, the Test_IsServerOnline_ShouldReturnTrue test method is expected to pass:

public bool IsServerOnline()
{
string address="localhost";
int port=8034;
SmppManager smppManager= new SmppManager(address, port);
bool isOnline=smppManager.TestConnection();
return isOnline;
}


[Fact]
public void Test_IsServerOnline_ShouldReturnTrue()
{
bool isOnline=IsServerOnline();

Assert.True(isOnline);
}

When the test is run and it passes, showing a green color depending on the test runner you are using, this provides an immediate feedback to you on the status of the code. This gives you confidence and inner joy that the code works correctly and behaves as it is intended to.

Refactoring is an iterative endeavor, where you continuously modify the code you have earlier written to pass the test until it has attained the state of production-ready code and that it fully implements the requirements and will work for all possible use cases and scenarios.