Test Driven Development For .Net Core

Barkın Kızılkaya
Geek Culture
Published in
4 min readNov 28, 2021

We can start testing with a simple question, Why do we need testing? And What is TDD?

I like the definition below

“TDD is software development process relying on software requirements being written as test cases before real code is written”

But this definition can change for you. I use TDD to organize my code and to make my code conform to solid principles. And of course, we are working with development teams in our companies. So someone’s changes can affect our code. To Protect code for future development we need to write a test. If we look at the refactoring part. Tdd helps us to refactor our codes.

Are there any disadvantages? Of course, Development Time! But for the initial code. After code is written. Then these disadvantages turn into an advantage.

This will be a long article. But I will try to explain my knowledge to you.

Lets Start,

Because of the topic. First we should create a test solution. Later It will change but for now solution will remain as test project.

Xunit Project Selection

Ok, whats is the project?

The project will be a rent a car app. There will be a web API and with this users can book a car. Sounds great!

For that, we should add the same great Nuget packages to the out project.
The first one is : Shouldly
The second one is : Moq

These packages are very common. You should have it!

The first answer that comes to mind. Well-organized code. Correct apply to solid patterns etc. For TDD you should get fail (Red) and then after writing your code and re-run the test it should give you success (Green).

Let’s look code a bit.

When we look function above. We need a function that takes a booking request and should return a booking response. And also there should be a service that has a BookCar function.

What are our needs?

  • Booking Request: should have Full Name, Email, Date.
  • Booking Response: should have Full Name, Email, Date.
  • Service and function to book car.

Initially, we don’t have that let us create,

I will add two projects “CarBookingApp.Core” this project will give function to book a car. And also it will have our request and response entity. The second project “CarBookingApp.Domain” for the domain models.

Firstly I want to create a base model for request and response. It seems that they have the same field in it.

After creating objects class we can focus on service. Let’s create a service for booking requests. I will add

After creating objects class we can focus on service. Let’s create a service for booking requests. I will add the CarBookingRequest processor to CarBookingApp.Core project. For now, we just test the method it will not book any car. Just for input check. If we do request it will return a response with input parameters. Later we will get the result from another service. But for now, it is ok for the current operation.

Now our first test is ready we wrote the test first then create classes and projects to act as the test.

Now we have green test. Lets write a test to check input parameters. Maybe It can be null is it ?

Now we have the green test. Let’s write a test to check input parameters. Maybe It can be null, is it? With this situation, we should throw an exception. But we write a test for it. It will be the red test.

Why? Because we need to check inputs but currently we are not. The solution to the problem we need to add an input check to BookCar function.

Now we are checking requests! Let’s run test again,

There is more code in the solution I will share these codes. But ı want to say something about Mock. In solution some Interfaces has mock why do we need to mock something? Because we want to check functions not all the system. At the end of the testing, we will check all functions but separately. But we can implement all the interfaces. For that mock help to us. When you get all solutions from my GitHub account. You will see there is a service that has a connection with the database. We need to mock that because we are not working with real data. We need a car list and we don’t have a DB yet. so we need to mock.

This is very brief information about TDD. But It can be a starting point. Feel Free to ask a question.

Take care yourselves.

--

--