Now let's bring those sticky posts that we have right here into our application using TDD.
So first of all, we will create a new flight with a seat capacity of three.
Then we will book a flight.
And finally the result will be that the original flight will only have two seats remained.
Right?
Because we started with three, we booked one, so he will have two left.
So just keep that three steps in mind right now.
So using the TDD approach, we will now create a flight.
Var flight equals to new flight and we will write down with a seat capacity of three.
There we go.
So we would pass that parameter here.
The second post was booking, so flight dot book.
Then we would submit an email address for example.
So.
Yannick At tutorials.
Newcomb So the real email address, by the way, and we would book one seat, so two parameters here,
book the flight for Yannick and book one seat, and the third call would be flight dot remaining number
of seats dot should dot be the result should be two.
So now this is the logic that we want to implement in our application and you can see how this even
helps building out well, a real application, right?
Even though we haven't included the core logic and the functionality, we have just sketched out how
we imagine our software should look like from a code side, right?
So now we will go through each arrow, create a flight, create a booking method, and finally take
care of that remaining number of seats property here, and then everything will work as expected.
And we have used the TDD approach.
So I hope that it now gets clearer and clearer how and why you would use TDD.
So you can see in the error log in the error list at the bottom side of the screen.
The first error we will now go step by step through them.
The first error is flight is a namespace, but it's used like a type.
However for flight referencing right now the namespace here.
But we need to create a new class now.
We need to create a flight class.
So let's create a new class library.
Right click on your solution and add a new project.
And again, we add a class library.
However, this time what we do to be even more professional, we create a domain library, but domain
for dot tests.
So this is how we call our project.
So everything that we now create for testing or regarding testing, for example, that flight class
is not a production code Right now.
After testing and after finalizing everything, we will refactor this and build a stable production
code from it.
Right?
So if you are in the scenario where you just writing down your new application or some new parts of
your application, right, you are well in a development phase, right?
And in a testing phase in that scenario here and for this, we are now creating a flight class.
And that flight class is for testing purposes right now.
And if we decide that it's working fine and we want to keep it, we will build a production code from
it and move it into a different production domain library.
But for now, it's testing.
So we keep it inside our test space.
And this is where we create the test library for our domain layer.
So let's hit next.
Dotnet six.
Here we have our domain dot test public class.
Let's just rename it right click rename.
Let's call it flight.
And inside there we have a public flight and seat capacity inside of the constructor because that's
the only way how we can really create a new flight and pass in the seat capacity.
Right?
So we need to create a constructor and set it here.
Great.
So now in our unit test project, we can still not use it.
First of all, we go into our flight test project, right click on it and add a new project reference
and select our test domain here.
Hit.
Okay, that's fine.
So now what we can do, we just save that.
So now what we can do is we can start using our domain dot test namespace.
And there we got that flight here.
So it's just conflicting with this namespace flight that we have right here.
So we can for sure rename it.
Or instead of that, we could also go ahead and say, well, domain dot test flight, right?
So you can go either way.
In that scenario, you for sure wouldn't need to start using the using directive here at the top side
of the screen.
It really depends on you.
So you can also go ahead and rename the namespace right inside of the flight test project.
And that's what I want to do because I want to keep it a little bit simplified.
So I will start using the domain test and then right click and rename flight test like this, right?
And you can see the reference was set to wrong.
That's why he used that.
But we can just write down flight now.
Awesome.
So now we proceeded here and we can see some new errors popping up.
Flight does not contain a definition for book.
So what we do next, we switch over again and now we create a book method.
Public void Book String v one string v two.
So v one and v two for values.
And afterwards, we don't care about real logic here, so we just throw.
There we go.
New not implemented exception.
Save it again.
Switch over to unit test.
You can see.
All right, here is an issue.
Right.
So he's trying to get a string, but he needs an integer.
So you can see I need to change that.
Right?
I mean, that was not related to TDD.
It's just default error handling anyway.
And the next thing that we have to do is to take care of that remaining number of seats property.
So switch over to the flight again and let's write down a property.
We can write down prop public.
INT Oh, and if you use a shortcut, simply press tab tab.
So let me do that again.
I don't want to skip it.
So you got prop, this is a short code, right?
And then hit tab tab and yeah, everything gets filled out.
So public int and then you can go through the fields using tab.
So public INT Let's go remaining number of seats, save switch over to unit test project and you can
see, all right, that works fine.
Now he's complaining about that should be because we don't use our fluent assertions.
So start using fluent assertions.
That's why we have implemented or included the NuGet package and now everything is working fine and
we can start the test, right?
So right click here, run test, give it some time to execute.
You can see in the background the application starts compiling and you can see that the test is failing.
And that's totally fine right now.
And we are only allowed to write production code to pass a failing test and emphasize this as much as
you can during this course, because it's the first rule of, all right, we are only allowed to write
production code to make a failing test pass.
So right now our test is failing and we need to fix this.
And for that we need to write production code.
Okay.
So if we check that here, take a look at the exception not implemented exception.
Let's switch over to our flight implementation here.
Let me just bring that here and let's get rid I will just close it.
We can just remove the not implemented exception right now.
So save it.
Go back again and start running the test one more time.
You can see it's still failing.
But this time we got some more information from fluent assertion.
So expected flood remaining number of seats to be two but found zero.
So we have a difference of minus two.
Now let's see how we can fix this because that's the actual implementation of the logic behind it or
the production code.
So just for yourself, take I mean, you can see this right here.
This should be your solution.
But just think about the possible implementation of how we can fix this failing test, right?
So think about the core logic that we now have to implement in the flight to make the test pass.
Now to make the test pass.
So in order to make this test pass, we for sure have to be the we for sure have to implement the logic.
So we initialize the flight with three seats.
Then we remove one seat and book for Yannick right here and then result.
So the remaining number of seats should be two.
Let's do this.
So here we create a new instance inside of the constructor of the flight class.
We say remaining number of seats is equal to the seat capacity, which we will provide in the call of
the instantiation.
So when we call the constructor, we say, Hey, the flight has like ten seats available remaining number
of seats will be ten then.
And when we book a flight and that's for sure, well, actually the decreasing part, we say remaining
number of seats decreased by the second value, which is actually then the amount of booked seats.
So that should be everything.