Building a .NET 6 API Using TDD

preview_player
Показать описание
In this video, we're going to build a .NET 6 Web API from scratch using test driven development! We'll cover creating a new solution, adding new web and unit test projects to the solution, and writing unit tests. In following videos in this series, we'll launch our application to a highly-available environment in AWS and look at various ways to configure, deploy, and monitor the application.

• 🤝 Support the Channel on Patreon! •
Patreon supporters get access to source code!

• 👋 Get in Touch! •

• 🧑🏽‍💻 Udemy Courses •

• 🕘 Timestamps

• 📚 Topics Covered in This Video
► .NET 6
► Web API Development
► TDD
► Test Driven Development
► Unit Testing
► HttpClient
► .NET Dependency Injection
► Inversion of Control
► SOLID Design Principles
► REST APIs
► dotnet CLI
Рекомендации по теме
Комментарии
Автор

Your content is always worth the wait. I trust your goal is to teach TDD by practice with this series, but I do wonder if the world needs more demonstrations of RESTful APIs...maybe a graphql api or a grpc application in the future? Just a thought. Looking forward to this series!

CripplingDuality
Автор

This is quite possibly one of the best C# TDD examples, with an API/WebRequest I've seen on Youtube.

Veretax
Автор

Thanks for the video! The code looks clean, but I want to give feedback to avoid pitfalls when going into more complex application testing:
- Unit testing controllers is rarely necessary. This is a controversial topic, but I recommend keeping controller layer as a pass-through for example with mediator pattern and use integration tests to cover the controller code. When doing the design this way there is usually no need to mock anything, just use a real data storage for the tests instead. This makes understanding the code and debugging the failure much easier.
- Test behavior, not implementation details. The main test here tests that a service calls an external API when calling the controller method. When testing the controller we are not interested how it gets the users, but the fact that it does so. Now the test is dependent on both controller and service interface code. When the service interface changes, the test breaks even though it will return the users correctly.
- When considering the two issues above and the fact that we still need an integration test to make sure that the users are returned correctly to the caller we start to see why unit testing controllers is not a good idea.
- Use setup (and teardown) methods to put shared initialization code into one place. The tests are almost the same and are already pretty long for what they do.
- TDD is much wider concept than unit testing. I see TDD as a tool to test how the system should behave. You start the process before writing any actual production code. This is tough for development-oriented mind. It is easier to think about testing "did it return 404" or "was that method called" instead of "were the requested details of the users stored returned back to the caller correctly".

The commentary, phasing and the code layout is good. Keep posting!

Greenthum
Автор

Thanks for a great video. I have kind of the same question as Cris Yapp though. It looks like you changed expectedResponse, string endpoint) method off camera so there you are no longer using the endpoint parameter. When left unchanged the test will fail for reasons I do not understand, even though the UsersService is not harcoded with the foo endpoint but using the config. It would be great to get some explanations for this.

girornsveinsson
Автор

Thanks for watching! What would you like to see next?

WesDoyle
Автор

I think I understand the most of ot, but I will have to retake that lesson later becasue the part about MockHttpRequest is a little bit too fast and complicated for me. I need to disassemble it into smaller pieces :D
Great video though!
But im my opinion there are things that you should change a bit :
-Go a little slower, I had to stop the recording several times in order to find myself and catchup.
-you should Go more into details, because you go like " I do that, and then that" bo no explenation why etc.
- sometimes you just click stuff really fast without additional comments. That would also be nice :)

Kudaty
Автор

A video on a web api/web dev project that is solid compliant would also be very much appreciated.

abbasfais
Автор

53:27 to remove all the unnecessary usings you can just press ctrl + k + e. Also, instead of replacing positions of brackets, you can set in IDE how ctrl + k +d should place them

damianfijoek
Автор

Hi Wes,
This is really a great video and helped me a lot to gain a new perspective to approach a problem statement or requirement.
You made me fall in love with the TDD approach :D
I would really like to see how we can further use this approach to create loosely coupled architecture with a database (SQL / No SQL). Will we see more continuation of this video?

abhishekdube
Автор

Hi Wes!, glad to see you back
We hope you create more videos this year..
Its always great to learn from you😊

VinuP
Автор

Thank you very much, your content help me because i need learn TDD and you taught very well now i can implement this patterns

pedrofreitaslima
Автор

Great video Wes! Glad to se your vids back!.. I just had one question though as I was following along, when you set up the mock HttpMessageHandler and then you created the overloaded function expectedResponse, string endPoint), I noticed that at first you created a custom HttpRequestMessage obj, then passed that in as a parameter in to the instead of using ItExpr.IsAny<HttpRequestMessage>(), then at the end when the test failed and you returned to that Mock it was changed back to and there wasnt a custom HttpRequestMessage obj being used... what did I miss? Keep up the content dude!

chrisyapp
Автор

Thumbs up! But I cannot really understand, why you fight againt your IDE in order to force Javascript-type bracket indentation. :)

miklosmoricz
Автор

Welcome back Wes, hope you are well. Strangely I was on your channel the other day and was wondering where you had gone!

ryadean
Автор

Never return 404 instead of empty lists, it's a bad API design. Do return 404 for not found objects, though (e.g. for GET /users/42 when no user with the ID of 42 exists).

IgorTimarac
Автор

I'd like to help out adding some hot keys and commands to make your coding even swifter:

Edit a class or object's name and replace it in all its callings: -}> Ctrl + R + R
Add a new file .cs (interface, class, etc) -}> ctrl + shift + A
Open the Quick actions and refactorings: -}> Ctrl + . (dot)

I'll keep adding more hot keys as I follow the video <3

EduardoMartinez-dmpp
Автор

Love it! Very cool! Can you make a video on how to become a .NET developer. In my area there are tons of jobs for .net and I would love to make the switch. Keep it up!

geeksy
Автор

Thank you for the tutorial but do you have an "INSERT" OCD? Why do you need to press "insert" every other keyboard press?

primavera
Автор

1:26:00 why are we setting up a near-duplicate of `SetupBasicGetResourceList` rather than just adding an `endpoint` parameter to the original method?

RoamingAdhocrat
Автор

Thanks a lot! Usually I decare tested class and mocks as local variables in test class and create it in constructor. In this case in test method all we have to do - setup mocks, execute method and do assert/verify. If we add new parameter into tested class constructor - refactoring is much easier.

eduardkoryagin