C
C#•11mo ago
Natro

What's correct unit testing?

In codebase I am working on some devs are writing unit tests that test Controller level - tests if endpoint returns 200 for example. They mock repository/DAL layer and ignore service layer. Wouldn't the correct thing to do be just mock the service layer and then test service layer functions separately and mock repo layer in that test?
15 Replies
Mayor McCheese
Mayor McCheese•11mo ago
You'll find a lot of opinions on this topic. For an aspnet core project my goal is to leverage the web application factory as much as possible, see examples at https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-8.0. From an isolated unit test perspective, I only want to test critical business logic. The things that are both provable, and represent a market advantage.
Integration tests in ASP.NET Core
Learn how integration tests ensure that an app's components function correctly at the infrastructure level, including the database, file system, and network.
DΣX
DΣX•11mo ago
there is nothing inherently wrong with testing the controller, when it is tested isolated (not testing the service at the same time). Only counter-argument is that the controller ideally should contain VERY little business-logic, which makes it less of a candidate for the more important metric: cover as much of your business (and I'm not talking 💲, but what the software is actually supposed to do) as possible. If your controller contains a lot of business, then its also a bad sign for the structure of your code. One other point is: the more you test, the more drag you put on development. If you need to change your test every time you touch your app, it will slow you down. So more is not always better. Put more effort in testing the right things, rather than testing ALL the things is beneficial.
Natro
NatroOP•11mo ago
In the end I decided to do integration testing on controller and mocking the repo layer. Mostly checking for http status responses based on inputs - showing what is the endpoint intended for essentially. Then doing additional unit tests for service (business) layer only in case if there is some business logic validation or additional logic that isn't tested in controller e2e tests. Then unit tests on DAL - if migrations went through correctly. I felt that doing unit tests on controller layer and mocking service layer was kind of pointless as I wouldn't really be testing anything, would just be adding lines of code and exactly as you said - it would put more drag on the development.
Mayor McCheese
Mayor McCheese•11mo ago
@Nox 🌺 ^^ look at this dude winning it right here.
Nox
Nox•11mo ago
Not necessarily true about the controller containing no business logic. The controller is usually the orchestrator of all of the service calls and data access and data manipulation needed to complete the endpoint. None of those things should depend on one another; the controller is the glue that holds it together
Mayor McCheese
Mayor McCheese•11mo ago
I mean about testing @Nox 🌺 we'll never change hearts and minds about the other.
DΣX
DΣX•11mo ago
there are definitly architectures that work like this. doesnt mean i have to like them 😄 all im saying is: test your important logic. when it is in the controller, you have to test the controller!
Nox
Nox•11mo ago
Catsip think that the controller can be aptly tested with e2e/integration
Mayor McCheese
Mayor McCheese•11mo ago
That's a deep rabbit hole.
Nox
Nox•11mo ago
<a:pocky_d_rabbit_hide:788223919139979264>
Mayor McCheese
Mayor McCheese•11mo ago
@Nox 🌺 okay that's the last one I'll say
Nox
Nox•11mo ago
<a:pocky_d_bunnipatpat:734603029369585756>
Mayor McCheese
Mayor McCheese•11mo ago
Today
DΣX
DΣX•11mo ago
wait till one hour is passed, then "today" has changed for somebody it can*
Want results from more Discord servers?
Add your server