Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
C# and .NET Core Test-Driven Development

You're reading from   C# and .NET Core Test-Driven Development Dive into TDD to create flexible, maintainable, and production-ready .NET Core applications

Arrow left icon
Product type Paperback
Published in May 2018
Publisher
ISBN-13 9781788292481
Length 300 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ayobami Adewole Ayobami Adewole
Author Profile Icon Ayobami Adewole
Ayobami Adewole
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Exploring Test-Driven Development FREE CHAPTER 2. Getting Started with .NET Core 3. Writing Testable Code 4. .NET Core Unit Testing 5. Data-Driven Unit Tests 6. Mocking Dependencies 7. Continuous Integration and Project Hosting 8. Creating Continuous Integration Build Processes 9. Testing and Packaging the Application 10. Other Books You May Enjoy

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.

You have been reading a chapter from
C# and .NET Core Test-Driven Development
Published in: May 2018
Publisher:
ISBN-13: 9781788292481
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime